我是一名初學者程序員,在Python學習,但我很肯定我有一個體面的把握如何使大多數事情運行,直到我遇到這個。由於我試圖運行一段代碼,其中嵌入了if..then語句,因此Python決定通過向我投擲一個曲線球,但不運行if ... then語句。當我嘗試運行該程序時,它所做的一切就是不斷運行我在while循環中的一行代碼,在if ... then語句之前。 下面的代碼:爲什麼我的WHILE循環不能在Python中運行?
def deg_or_rad():
global deg_rad
deg_rad = False
while deg_rad == False:
query = raw_input("Are you working in 'degrees' or 'radians'? > ").lower
if query == "deg" or \
query == "degrees":
deg_rad = "deg"
print "Cool! I like degrees."
elif query == "rad" or \
query == "radians":
deg_rad = "rad"
print "Cool! I like radians."
else:
"Umm... I'm confused..."
我已經嘗試了一些其他變量while循環,如:
def deg_or_rad():
global deg_rad
deg_rad = False
while_variable = True
while while_variable == True:
query = raw_input("Are you working in 'degrees' or 'radians'? > ").lower
if query == "deg" or \
query == "degrees":
deg_rad = "deg"
print "Cool! I like degrees."
while_variable = False
elif query == "rad" or \
query == "radians":
deg_rad = "rad"
print "Cool! I like radians."
while_variable = False
else:
"Umm... I'm confused..."
人有什麼想法?這點我真的很困惑。
你應該真的'while while_variable == True'用'while while_variable'替換'while deg_rad == False' with'while not deg_rad'。或者更好的是,只需在兩個循環中使用'while True'並使用'break'來結束循環而不是設置標誌變量。 –