简体   繁体   English

'AttributeError: 'tuple' 对象没有属性 'lower'' - 为什么 '.lower()' 方法在 Python 中不起作用?

[英]'AttributeError: 'tuple' object has no attribute 'lower'' - Why doesn't the '.lower()' method doesn't work here in Python?

I am currently learning python and doing the exercises in the module.我目前正在学习 python 并在模块中做练习。 I have learned previously how to remove any case letter in the string, so it will be easier for the user to use it later on.我之前已经学习了如何删除字符串中的任何大小写字母,因此用户以后可以更轻松地使用它。 But it seems like when I applied the ' XXX.lower() ' method to my code, it doesnt work.但似乎当我将 ' XXX.lower() ' 方法应用于我的代码时,它不起作用。 It works just fine without the '.lower()' method but I really want to know why it doest work ?它在没有 '.lower()' 方法的情况下工作得很好,但我真的想知道为什么它不起作用? Here's my code :这是我的代码:

# 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 is a tuple, .lower() is supposed to be called on a string. bird_names是一个元组, .lower()应该在字符串上调用。 You might want to consider something like您可能需要考虑类似

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

and then you can use然后你可以使用

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

Because bird names is a tuple object not a string.因为鸟名是一个元组对象而不是一个字符串。 In order for this to work what you can do is updating or overwriting the existing tuple with a list comprehension or generating a new variable.为了使其工作,您可以做的是使用列表理解更新或覆盖现有元组或生成新变量。 Instead of bird_names.lower() you can use the new list or tuple in the conditions.您可以在条件中使用新列表或元组,而不是bird_names.lower()

bird_names_lower = [bird.lower() for bird in bird_names]
  • bird_names is a list object bird_names是一个列表对象
  • .lower() is a string function .lower()是一个字符串函数

maybe something like this can help you:也许这样的事情可以帮助你:

if bird_guess.lower() in [word.lower() for word in bird_names]:

You are trying to use .lower() , which is a string method, on the tuple bird_names .您正在尝试在元组bird_names上使用.lower() ,这是一个字符串方法。 If you want the strings in bird_names to be lower case, either hard code them as lower case or modify them with something like a tuple comprehension.如果您希望bird_names的字符串为小写,请将它们硬编码为小写或使用元组理解之类的东西修改它们。

bird_names = tuple(x.lower() for x in bird_names)

Then you can test bird_guess.lower() for membership in bird_names然后,您可以测试bird_guess.lower()中的bird_names成员资格

if bird_guess.lower() in bird_names:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python AttributeError:“ tuple”对象没有属性“ lower” - Python AttributeError: 'tuple' object has no attribute 'lower' AttributeError: 'tuple' 对象没有属性 'lower' - AttributeError: 'tuple' object has no attribute 'lower' Python Pandas to_datetime AttributeError: 'tuple' object 没有属性 'lower' - Python Pandas to_datetime AttributeError: 'tuple' object has no attribute 'lower' 具有AttributeError:“ tuple”对象的Python脚本没有属性“ lower” - Python script with AttributeError: 'tuple' object has no attribute 'lower' AttributeError: 'tuple' object 没有属性 'lower' sentiword - AttributeError: 'tuple' object has no attribute 'lower' sentiword AttributeError: 'NoneType' 对象没有属性 'lower' python - AttributeError: 'NoneType' object has no attribute 'lower' python AttributeError:'NoneType'对象在python中没有属性'lower' - AttributeError: 'NoneType' object has no attribute 'lower' in python AttributeError: 'list' object 没有属性 'lower' - Python - AttributeError: 'list' object has no attribute 'lower' - Python Python pandas 较低的数据 AttributeError: 'Series' object has no attribute 'lower' - Python pandas lower data AttributeError: 'Series' object has no attribute 'lower' 理解 to_datetime AttributeError: 'tuple' 对象没有属性 'lower' - Understand to_datetime AttributeError: 'tuple' object has no attribute 'lower'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM