Emile
Emile

Reputation: 373

while loop with 2 conditions

I have been struggling with a small loop. I do not know why my while loop does not work. If I remove one of the 2 conditions it works, but when both are together it does not...

temp = input("Choose between Fahreneit degrees 'f'' key or Celsius degrees 'c' key ")

while temp != "f" or temp != "c" :
 temp = input("you must choose f or c")

if (temp == "f") :
 print("you choosed Fahreneit")
 degF = int(input("What is the temperature outside?"))
 print("It seems that the temperature is around" , (0.56 * (degF - 32)) ,"Celsius from your Fahreneit sample")

elif (temp == "c") :
 print("you choosed Celsius")
 degC = int(input("What is the temperature outside?"))
 while degC < -273 or degC > 5500 :
  degC = int(input("Please enter a correct value"))

print("it seems that the temperature is around" , (1.8 * (degC) + 32) , "Fahreneit from your Celsius sample")

Upvotes: 0

Views: 76

Answers (1)

Kevin
Kevin

Reputation: 76184

temp != "f" or temp != "c" is not the opposite of temp == "f" or temp == "c". See De Morgan's laws for guidance regarding the negation of boolean expressions.

Try:

while temp != "f" and temp != "c":

Or just:

while not (temp == "f" or temp == "c"):

Or skip the boolean headache entirely:

while temp not in ("f", "c"):

Upvotes: 4

Related Questions