Berly
Berly

Reputation: 33

Python I can't exit the while loop even though the condition is satisfied

I tried to write a code that calculates the amount of bill everyone should pay after adding the tip but I wanted to limit the user to certain tip percentages and give them an error if they picked something else.

So, I came up with this:

print("Welcome to the tip calculator.")

bill = float(input("What was the total bill?"))

people = int(input("How many people to split the bill?"))

perc = int(input("What percentage tip would you like to give? 10, 12, or 15?"))


total = float((bill + (bill * perc / 100)) / people)


while perc != 10 or perc != 12 or perc != 15:
    print("Error, please chose one of the given percentages.")
    perc = float(input("What percentage tip would you like to give? 10, 12, or 15?"))


else:
    print("Everyone should pay", total)

But, even though I enter 10, 12, or 15 I get the " Error, please chose one of the given percentages." message. What should I do?

Upvotes: 3

Views: 227

Answers (4)

PawelU
PawelU

Reputation: 11

use:

while perc not in [10, 12, 15]:

is better (see answers above),

but to understand You problem try this:

while perc != 10 or perc != 12 or perc != 15:
    perc = float(input("What percentage tip would you like to give? 10, 12, or 15?"))
    print("10: ", perc != 10, "12: ", perc != 12, "15: ", perc != 15, "OR: ", perc != 10 or perc != 12 or perc != 15)`

Result is:

What percentage tip would you like to give? 10, 12, or 15?10
10.0
10:  False 12:  True 15:  True OR:  True
What percentage tip would you like to give? 10, 12, or 15?12
12.0
10:  True 12:  False 15:  True OR:  True
What percentage tip would you like to give? 10, 12, or 15?15
15.0
10:  True 12:  True 15:  False OR:  True

or is logical sum, in every numbers two of the condition is tru

True + True + False = True

True + False + True = True

False + True + True = True

Upvotes: 1

mhhabib
mhhabib

Reputation: 3121

Your condition while is messed up and also you use else that's why you're getting error. Try instead of using a loop.

print("Welcome to the tip calculator.")
bill = float(input("What was the total bill?"))
people = int(input("How many people to split the bill?"))
perc = int(input("What percentage tip would you like to give? 10, 12, or 15?"))
total = float((bill + (bill * perc / 100)) / people)
while perc not in [10, 12, 15]:
    print("Error, please chose one of the given percentages.")
    perc = float(input("What percentage tip would you like to give? 10, 12, or 15?"))
else:
    print("Everyone should pay", total)

Upvotes: 1

Thierry Lathuille
Thierry Lathuille

Reputation: 24288

Your condition should be

if perc != 10 and perc != 12 and perc != 15:   

You want it satisfied if all of these 3 conditions are satisfied. Using or, the whole condition would be satisfied if any of them was.

You could write is in a shorter manner:

if perc not in [10, 12, 15]:

Upvotes: 3

Nullman
Nullman

Reputation: 4279

Your logic is messed up. if perc=10 then it must NOT be perc=12 but you run if either of those are met.... try changing your or to and or better yet try:

while perc not in [10, 12, 15]:
    print("Error, please chose one of the given percentages.")
    perc = float(input("What percentage tip would you like to give? 10, 12, or 15?"))

Upvotes: 1

Related Questions