-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtip.py
More file actions
34 lines (26 loc) · 978 Bytes
/
tip.py
File metadata and controls
34 lines (26 loc) · 978 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
26
27
28
29
30
31
32
33
34
# Tip Calculator: calculates how much tip to leave based on meal cost and percentage
def dollars_to_float(d: str) -> float:
"""
Convert a string formatted as $##.## into a float.
Example: "$50.00" -> 50.0
"""
return float(d.replace("$", ""))
def percent_to_float(p: str) -> float:
"""
Convert a string formatted as ##% into a float (decimal).
Example: "15%" -> 0.15
"""
return float(p.replace("%", "")) / 100
def main():
# Ask the user for meal cost
dollars = dollars_to_float(input("How much was the meal? "))
# Ask the user for tip percentage
percent = percent_to_float(input("What percentage would you like to tip? "))
# Calculate the tip
tip = dollars * percent
# Output the result (rounded to 2 decimals)
print(f"Leave ${tip:.2f}")
# farewell message
print("Thank you! I hope I've been helpful 🙂")
if __name__ == "__main__":
main()