我的目标是打开狗文件,将其转换成列表,然后让用户输入一种狗,如果它与列表中的狗名匹配,则说它是正确的。
dog_file = open("Dogs.txt", "r")
dogs = dog_file.readlines()
print(dogs)
data = input("Enter a name: ")
if data == dogs:
print("Success")
else:
print("Sorry that didn't work")
请您参考如下方法:
dogs 是字符串列表,而 data 是单个字符串。您想要使用 in 运算符检查 data 是否包含在 dogs 中:
if data in dogs:
# do sth
