2014-09-19 60 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