Home > other >  Tip calculator with different tips amount
Tip calculator with different tips amount

Time:10-25

Below is my code, I am trying to get tip amount with different tip option and tip amount should be a 2 decimal places but didn't get the result

bill_amount = float(input('How much is the bill? $'))
ten = float((bill_amount / 100) * 10)
fifteen = float((bill_amount / 100) * 15)
twenty = float((bill_amount / 100) * 20)
tip_option = input('Tip Options: ')
print('10% Tip amount is', {'ten.2f'})
print('15% Tip amount is', {'fifteen.2f'})
print('20% Tip amount is', {'twenty.2f'})

CodePudding user response:

I think this is a good case to use a multiline f-string:

bill_amount = float(input('How much is the bill? $'))
ten = float((bill_amount / 100) * 10)
fifteen = float((bill_amount / 100) * 15)
twenty = float((bill_amount / 100) * 20)
tip_option = input(f'''Tip Options: 
10% Tip amount is', {ten:.2f}
15% Tip amount is', {fifteen:.2f}
20% Tip amount is', {twenty:.2f}
''')

The reason I suggest this approach is that you want to store the option chosen by the user in tip_option after being asked for the tip amount.

CodePudding user response:

In python3, you can format the string like:

print('10% Tip amount is {amount:.2f}'.format(amount = ten))

See this documentation page for .format for more info: enter image description here

  • Related