Home | Lecture 0 | Problem 0.1 | Problem 0.2 | Problem 0.3 | Problem 0.4 | Problem 0.5
In the United States, it’s customary to leave a tip for your server after dining in a restaurant, typically an amount equal to 15% or more of your meal’s cost. Not to worry, though, we’ve written a tip calculator for you, below!
def main():
dollars = dollars_to_float(input("How much was the meal? "))
percent = percent_to_float(input("What percentage would you like to tip? "))
tip = dollars * percent
print(f"Leave ${tip:.2f}")
def dollars_to_float(d):
# TODO
def percent_to_float(p):
# TODO
main()Well, we’ve written most of a tip calculator for you. Unfortunately, we didn’t have time to implement two functions:
dollars_to_float, which should accept astras input (formatted as$##.##, wherein each#is a decimal digit), remove the leading$, and return the amount as afloat. For instance, given$50.00as input, it should return50.0.percent_to_float, which should accept astras input (formatted as##%, wherein each#is a decimal digit), remove the trailing%, and return the percentage as afloat. For instance, given15%as input, it should return0.15.
Assume that the user will input values in the expected formats.
- Recall that
inputreturns astr, per https://docs.python.org/3/library/functions.html#input. - Recall that
floatcan convert astrto afloat, per https://docs.python.org/3/library/functions.html#float. - Recall that a
strcomes with quite a few methods, per https://docs.python.org/3/library/stdtypes.html#string-methods. Demo
From the root of your repository execute cd 0-FunctionsVariables So your current working directory is ...
/0-FunctionsVariables $:Next execute
mkdir tipto make a folder called tip in your codespace.
Then execute
cd tipto change directories into that folder. You should now see your terminal prompt as /0-FunctionsVariables/tip $. You can now execute
code tip.pyto make a file called tip.py where you’ll write your program.
Here’s how to test your code manually. At the tip/ $ prompt in your terminal: :
-
Run your program with
python tip.py. Type$50.00and press Enter. Then, type15%and press Enter. Your program should output:Leave $7.50
-
Run your program with
python tip.py. Type$100.00and press Enter. Then, type18%and press Enter. Your program should output:Leave $18.00
-
Run your program with
python tip.py. Type$15.00and press Enter. Then, type25%and press Enter. Your program should output:Leave $3.75
At the /0-FunctionsVariables/tip $ prompt in your terminal:
git add -A Add all changed files in the repository to be committed
git commit -m “Upload completed tip.py“Commit all changes in the REPO with the comment “Upload completed tip.py“ note: If the file is not complete, adjust the comment to describes what is being commited
git push Push all changes to the REPO