Excercises
2.1 - Given the following restaurant bill:
- bill: 84.50
- tip rate: 0.18
- Number of people: 3
Write a program (split-bill.py) that calculates how much each person will pay, rounding to
2 decimal points.
2.2 — A shop sells items at 4.75 each. Write a program (cost.py) that
prompts the user to enter a quantity and stores it in a variable.
The program then computes the subtotal, adds 13% tax, and prints the subtotal, the tax, and the grand total, each rounded to 2 decimals.
2.3 — Write a program (temperature.py) that converts from Celsius to Fahrenheit
using the following formulate: F = C * 9/5 + 32.
The program should prompt the user to enter degrees in Celsius, compute the conversion, and print both.
2.4 — Write a program (time-split.py) to convert a given number of minutes into
hours and minutes. For example, 500 minutes would give 8 h 20 m. The program should
prompt the user to enter the number of minutes, store it in a variable, compute the
split and print the value the user entered and the result of the conversion.
2.5 — Write a program (number-swap.py) that swaps two variables. The program should
prompt the user to enter the value for a and store it in a variable. Then prompt the user
to enter the value for b and store it in a variable.
Then, using a single statement, swap the two variables.
Finally, print the "before" and "after" showing the values of a and b before and after
the swap.
Hints
- 2.2 — tax is
subtotal * 0.13; grand total issubtotal + tax.round(x, 2). - 2.3 — write
9/5(true division) not9//5(which is1!). This is a classic bug. - 2.5 — use multi-variable assignment syntax.
- Remember to convert user input to the appropriate type if used in calculations.
Homework
H2.1 — Write change.py: given an integer number of cents (e.g. 287), print how many
dollars and cents that is (2 dollars and 87 cents) using // and %.
The program should prompt the user for a number of cents above 100, store it in a variable, then compute the conversion and print the results.
H2.2 — Write powers.py that prints the first few powers of 2 (2**0 through 2**10).
Use one print statement per line.