2014-09-19 61 views
0

我一直在努力與一個小循環。我不知道爲什麼我的while循環不起作用。如果我刪除的2個條件它的作品之一,但是當兩者都一起它不...while 2條件環

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") 
+3

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

或者只是:

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

或跳過完全布爾頭痛 「我一直在掙扎了小船尾。」 - 經典。 – Ideasthete 2014-09-19 14:34:58

+1

一個空間是不夠的 - 爲了可讀性遵循[風格指南](http://legacy.python.org/dev/peps/pep-0008/#indentation)並縮進四個空格 – jonrsharpe 2014-09-19 14:41:15

回答

4

temp != "f" or temp != "c"不是temp == "f" or temp == "c"相反。有關否定布爾表達式的指導,請參閱De Morgan's laws

嘗試:

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

非常感謝凱文,我有說我不知道​​德摩根的法律。答案是你列表中的第一次嘗試。 – E3E 2014-09-19 14:42:27