Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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. 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. It works just fine without the '.lower()' method but I really want to know why it doest work ? 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.")
like image 349
Jabri Juhinin Avatar asked Nov 19 '25 04:11

Jabri Juhinin


2 Answers

bird_names is a tuple, .lower() is supposed to be called on a string. 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:
    ....
like image 167
Andrew Wei Avatar answered Nov 20 '25 18:11

Andrew Wei


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.lower() for bird in bird_names]
like image 39
alparslan mimaroğlu Avatar answered Nov 20 '25 16:11

alparslan mimaroğlu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!