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.")
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:
....
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With