Python if语句未正确获取输入

Python if语句未正确获取输入,python,Python,你好,我有这个密码 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 categ

你好,我有这个密码

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',它不会打破循环,我也不知道为什么它不工作
您实际上没有调用
.lower()
函数。改用
str(category).lower()

我只是在评论这一点,让大家知道我找到了答案

您实际上并没有调用
.lower()
函数。请改用
str(category).lower()
来代替。非常感谢您,您的
break
语句是多余的,因为由于
x=False
,您无论如何都要跳出循环。Python中的一种常见做法是
,而True:。。。由于不再需要
x
变量,因此可以将其简化。如果不想打印当前类别,可以执行以下操作:
categories=list(takewhile(lambda L:L.lower()!='ex',iter(lambda:input('Next:'),None))