简体   繁体   中英

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?

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]:

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?"))

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM