工作/ ELIF我有这样的代码如果未如预期在Python 2.7
print "Choose a category: Animals, Jobs or Cars?"
while True:
cat = str(raw_input())
if cat.lower() == "animals":
wordfile = open("Animals.txt")
break
elif cat.lower == "jobs":
wordfile = open("Jobs.txt")
break
elif cat.lower == "cars":
wordfile = open("Cars.txt")
break
else:
print "That's not a valid category!"
wordlist = [line.split("\n") for line in wordfile]
print wordlist
当我输入的动物,它完美的作品和我的打印文件的内容。然而,当我输入工作或汽车时,它告诉我,这不是一个有效的类别(它跳过elif的,直接去其他地方),我不明白为什么。
你在'elif'语句中'cat.lower'丢失了括号 – LMc
另外,为什么要使用'cat = str(raw_input())'? 'raw_input()'本身将返回一个字符串。 –