Homework 1

Due: October 14, 2024
Points: 100


In the given examples, what you type is in red and the program prints what is in black. Your program output should look exactly like the output in the examples, except that what you type won’t be in red. Also, the symbol “↵” is a newline (return or enter keys).

  1. (25 points) A restaurant charges an 18% tip on all meals. The city charges an 8.5% sales tax on the price, which includes the tip. Write a program that asks the user how much their meal cost, and calculate what they must pay. Print that amount, to 2 decimal digits. Continue until the user enters 0.

    Hint: If you store the number to print in the variable v, use the following statement to print it:

    print("You need to pay %.2f" % (v))
    
    To turn in: Please turn in the program in the file meal.py.
    Sample inputs and outputs:
    Cost of meal (0 to quit): 9.21↵
    You need to pay 11.79
    Cost of meal (0 to quit): 10↵
    You need to pay 12.80
    Cost of meal (0 to quit): 0↵ 
    
  2. (25 points) Write a program to calculate the letter grade for an exam. The user will enter the points obtained on the exam and the total points possible. From this, determine and print the grade using the following rules:
    1. A score of at least 90% is an A
    2. A score at least 80% and up to but not including 90% is a B
    3. A score at least 70% and up to but not including 80% is a C
    4. A score at least 60% and up to but not including 70% is a D
    5. A score less than 60% is an F
    Prompt the user for the score and the total points possible. Reject any score that is greater than the total. You may assume the score and total are entered as integers.

    To turn in: Please turn in the program in the file grade.py.
    Sample inputs and outputs:

    Enter score: 160↵
    Total possible: 200↵
    A score of 160 out of 200 is a B
    
    Enter score: 72↵
    Total possible: 200↵
    A score of 72 out of 200 is an F
    
    Enter score: 90↵
    Total possible: 121↵
    A score of 90 out of 121 is a C
    
    Enter score: 75↵
    Total possible: 50↵
    The score 75 is greater than the total possible, 50
    

  3. (25 points) The Gregory-Leibniz series produces an approximation of π. The series is:

    Compute the value of this sequence after 1,000,000 terms, and print the result after each 100,000 terms have been computed. Use the following print statement:

    print("%7d\t%.15f" % (count, sum))
    
    where count is the number of iterations and sum is the value of the first count terms.

    To turn in: Please turn in the program in the file pi.py.
    Sample output: only the first 4 lines are shown

     100000	3.141582653589720
     200000	3.141587653589762
     300000	3.141589320256464
     400000	3.141590153589744
    
  4. (25 points) Debugging: The program ftok.py should convert temperatures in Fahrenheit to temperatures in Kelvin — recall the formula is:
    where k is the temperature as Kelvin and f is the temperature in Fahrenheit. It is to print out the temperatures as decimal numbers, rounded to 2 decimal places if they are not integers, and to reject any input if it converts to a negative Kelvin temperature. But it doesn’t work — indeed, it won’t even run to completion!

    Find the bug(s) and fix it (them). Put in your comments what you changed.

    Hint: The function to round a number x to two places is round(x, 2).
    To turn in: Please turn in the program in the file ftok-fixed.py.
    Sample output: from the correctly working program:

    Enter temperature in degrees Fahrenheit> 100↵
    100.0 Fahrenheit is 310.93 Kelvin
    
    Enter temperature in degrees Fahrenheit> -459.67↵
    -459.67 Fahrenheit is 0.0 Kelvin
    
    Enter temperature in degrees Fahrenheit> -600↵
    That temperature is below 0 Kelvin, which is impossible
    

UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 235A, Computer and Information Security
Version of September 14, 2024 at 8:38PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh