Site Tools


marc:linux:python:voorbeelden:tip_calculator

Tip calculator

Gebruik van “round” en de “fString”;

round kan gebruikt worden om getallen (in onderstaand voorbeeld bedragen die je altijd wilt afronden) af te ronden.

print(round(3.141592653589)) geeft resultaat 3.
print(round(3.141592653589, 2)) geeft resultaat 3.14.
print(round(3.60, 2)) geeft resultaat 3.6

Het laatste voorbeeld hierboven is soms ongewenst; wanneer je bedragen wilt weergeven wil je immers dat er 3.60 komt te staan, niet 3.6.

print("Welcome to the tip calculator.")
total_bill = float(input("What was the total bill?\n"))
percentage = int(input("What percentage tip would you like to give? 10, 12, or 15?\n"))
people = int(input("How many to split the bill?\n"))
bill_including_tip = total_bill + (total_bill / 100 * percentage)
individual_bill = round((bill_including_tip / people) , 2)
print(f"Each person should pay: ${individual_bill}.")
marc/linux/python/voorbeelden/tip_calculator.txt · Last modified: by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki