-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtip.py
More file actions
25 lines (16 loc) · 1021 Bytes
/
tip.py
File metadata and controls
25 lines (16 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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 a str as input (formatted as $##.##, wherein each # is a decimal digit),
# remove the leading $, and return the amount as a float. For instance, given $50.00 as input, it should return 50.0.
# percent_to_float, which should accept a str as input (formatted as ##%, wherein each # is a decimal digit),
# remove the trailing %, and return the percentage as a float. For instance, given 15% as input, it should return 0.15.
# Assume that the user will input values in the expected formats.
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):
return round(float(d.replace("$", "")), 1)
def percent_to_float(p):
return float(p.replace("%", ""))/100
main()