0

so I'm writing a code for a tipping system that on the basis of percentages calculates how much the tip added to the bill will be. I wanted to add a condition if the user wanted to give no tip

print("welcome to the tip calculator, hope you had a good time")
bill=input("how much was your bill\n")
tip=input("how much would you like to tip?\n No tip, 10% , 15%, 20%\n")
if tip == "No tip":
  print("Thank you for dining.") 
else: crowd= input("how many people will split the bill?\n")
new_tip= float(bill)* int(tip)/100
bill=int(bill) + int(new_tip)
pay= int(bill) / int(crowd)
print(f"Each person should pay  {pay}.")
print("Thank you.")

It works fine when put in any number but when I put no tip, this is the error

Traceback (most recent call last): File "main.py", line 15, in new_tip= float(bill)* int(tip)/100

ValueError: invalid literal for int() with base 10: 'No tip'

1
  • You are trying to convert the string "No tip" to a number. What do you expect to happen? Can you think of a way to avoid this? Commented Sep 28, 2022 at 16:36

1 Answer 1

1

I believe your issue is with your indentation. When I copied your code and then indented the block of code after the "else" clause, the program ran fine.

print("welcome to the tip calculator, hope you had a good time")
bill=input("how much was your bill\n")
tip=input("how much would you like to tip?\n No tip, 10% , 15%, 20%\n")
if tip == "No tip":
    print("Thank you for dining.") 
else: 
    crowd= input("how many people will split the bill?\n")
    new_tip= float(bill)* int(tip)/100
    bill=int(bill) + int(new_tip)
    pay= int(bill) / int(crowd)
    print(f"Each person should pay  {pay}.")
    print("Thank you.")

Here is some sample output.

@Una:~/Python_Programs/Dining$ python3 Dining.py 
welcome to the tip calculator, hope you had a good time
how much was your bill
100
how much would you like to tip?
 No tip, 10% , 15%, 20%
No tip
Thank you for dining.

Give that a try.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.