Home > Blockchain >  How can I make one print statement for all of the different scenarios together of a restaurant tip b
How can I make one print statement for all of the different scenarios together of a restaurant tip b

Time:10-31

My instructions are: You are going to write a tip-calculating program that does the following:

  • Asks the user to enter the subtotal of a restaurant bill
  • Asks the user if they wish to enter a tip and if they do, let them choose to enter the tip by a specific amount, or by percentage of the subtotal.
  • Display the receipt: Subtotal, Taxed Owed (13%), Tip, Final Total

I feel like I have too many print statements. How can I make it so there is one that works for all of them?

subtotal= float(input("Please enter the subtotal of your bill:"))
print("\nPlease select an option:")
print("1. Enter tip percentage (%)")
print("2. Enter tip amount ($)")
print("3. No tip")
choice= float(input("How would you like to tip?"))
tax= subtotal*0.13

if choice == 1:
    tip_percentage= float(input("Please enter the percentage you would like to tip (%):"))

    if tip_percentage > 0:
      percent_decimal= tip_percentage/100
      tip_1= subtotal*percent_decimal
      finaltotal_1= subtotal   tax   tip_1

    if subtotal > 0:
        print("\nSubtotal:", (round(subtotal,2)))
        print("Tax Owed:", (round(tax,2)))
        print("Tip:", (round(tip_1,2)))
        print("Final total:", (round(finaltotal_1,2)))

    else:
       print("Please try again.")

if choice == 2:
    tip_2= float(input("Please enter the tip amount:"))

    if tip_2 > 0:
      finaltotal_2= subtotal   tax   tip_2

      if subtotal>0 and tip_2>0:
        print("\nSubtotal:", (round(subtotal,2)))
        print("Tax Owed:", (round(tax,2)))
        print("Tip:", (round(tip_2,2)))
        print("Final total:", (round(finaltotal_2,2)))

    else:
          print("Please try again.")

if choice == 3:
    tip_3= 0
    finaltotal_3= subtotal   tax   tip_3

    if subtotal>0:
      print("\nSubtotal:", (round(subtotal,2)))
      print("Tax Owed:", (round(tax,2)))
      print("Tip:", (round(tip_3,2)))
      print("Final total:", (round(finaltotal_3,2)))
      
    else:
          print("Please try again.")

CodePudding user response:

Use function:

def my_print():
    print("\nSubtotal:", (round(subtotal,2)))
    print("Tax Owed:", (round(tax,2)))
    print("Tip:", (round(tip_2,2)))
    print("Final total:", (round(finaltotal_2,2)))

And call this function from your code.

Also you can use one “print” and use multi lines in it.

CodePudding user response:

Since you repeat the 4 code statements 3 times, it would be better to have them as a function.

    def print_total(subtotal, tax, tip, finaltotal):
        print("\nSubtotal:", (round(subtotal,2)))
        print("Tax Owed:", (round(tax,2)))
        print("Tip:", (round(tip_3,2)))
        print("Final total:", (round(finaltotal_3,2)))

This code is like creating your own "print()" but instead of writing "print()" you write the name of your function and any arguments. In this case you have 4 arguments (in the parenthesis) and when you call the function they are passed to it as variables. In this case they are used in the print statements.

The rest of your code is fine. You could maybe replace the if statements with the (relatively) new "switch" statement. (link) But it is just for readability and won't change how your code runs.

  • Related