2017-09-16 88 views
-1

你好,我有這樣的代碼Python的if語句不正確服用輸入

print('Please enter each category you would like (type ex to exit)') 
x = True 
while x == True: 
    category = input('Next:') 
    if str(category).lower != 'ex': 
     categories.append(category) 
     print ('Here are your current categories' + str(categories)) 
    else: 
     x = False 
     break 

,如果我輸入「EX」不破環,我想不通爲什麼它不工作

+5

你實際上不調用'.lower()'函數。改用'str(category).lower()'。 – Delgan

+0

ahhhhhh非常感謝你 – skalerz

+1

另外,由於'x = False',你的'break'語句是多餘的,因爲無論如何你都要走出循環。 Python中的一個常見做法是「while True:... break」,它簡化了一些事情,因爲你不再需要'x'變量。 – Delgan

回答

1

Delgan說,這個 你實際上不會調用.lower()函數。改爲使用str(category).lower()。 和我只是評論這個,所以我知道我找到了答案