gpt4 book ai didi

python - '属性错误 : 'tuple' object has no attribute 'lower' ' - Why doesn't the '.lower()' method doesn't work here in Python?

转载 作者:行者123 更新时间:2023-12-05 01:06:03 26 4
gpt4 key购买 nike

我目前正在学习 python 并在模块中做练习。我之前已经学习过如何删除字符串中的任何大小写字母,这样用户以后使用它会更容易。但似乎当我将“XXX.lower() ”方法应用于我的代码时,它不起作用。没有 '.lower()' 方法它工作得很好,但我真的很想知道为什么它不起作用?这是我的代码:

# bird names list
bird_names = ("Parrot", "Owl", "Pigeon", "Crow", "Peacock", "Hen")

# bird guess
bird_guess = input("Enter a bird name (Guess 1): ")

# nested conditions starts here
if bird_guess.lower() in bird_names.lower():
print("Yes, 1st try!")
else:
bird_guess = input("Enter a bird name (Guess 2): ")
if bird_guess.lower() in bird_names.lower():
print("Yes, 2nd try!")
else:
bird_guess = input("Enter a bird name (Guess 3): ")
if bird_guess.lower() in bird_names.lower():
print("Yes, 3rd try!")
else:
print("Sorry, out of tries.")

最佳答案

bird_names 是一个元组, .lower() 应该在一个字符串上调用。您可能需要考虑类似

bird_names = ("Parrot", "Owl", "Pigeon", "Crow", "Peacock", "Hen")
bird_names = [name.lower() for name in bird_names]

然后你就可以使用了

if bird_guess.lower() in bird_names:
....

关于python - '属性错误 : 'tuple' object has no attribute 'lower' ' - Why doesn't the '.lower()' method doesn't work here in Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69909708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com