Having an issue with conditional after getting strings lower
Question:
I am a noob in coding and still learning the basics. I’ve been messing with the code and I have reached this situation where the code is just broken. I want to make a simple conditional but lowering the input of the user to make sure the conditional works. I have asked Chat GPT, and two different "AI Code Checkers" and all these tools tell me that the code is just fine, and it looks like it. But obviously is not. Can anyone explain to me what is wrong?
while True:
main_colors = ["cian", "amarillo", "magenta"]
user_input = input("Introduce el color (en espaƱol): ")
user_input_lower = user_input.lower
if user_input_lower in main_colors:
print("Es un color primarion")
else:
print("No es un color primarion")
Thanks in advance!
Answers:
Here is the error in your code:
You have written: user_input_lower = user_input.lower
The correct syntax is user_input_lower = user_input.lower()
lower()
is a method / function that you are performing on the string:: user_input
. When you write .lower
it is trying to look for a class variable lower in the class definition which does not exist and hence you get the error.
First of all your indentation is wrong you need to indent your whole code after the while condition, then you have to use the parenthesis after "lower" user_input.lower()
. That was your only mistakes.
I am a noob in coding and still learning the basics. I’ve been messing with the code and I have reached this situation where the code is just broken. I want to make a simple conditional but lowering the input of the user to make sure the conditional works. I have asked Chat GPT, and two different "AI Code Checkers" and all these tools tell me that the code is just fine, and it looks like it. But obviously is not. Can anyone explain to me what is wrong?
while True:
main_colors = ["cian", "amarillo", "magenta"]
user_input = input("Introduce el color (en espaƱol): ")
user_input_lower = user_input.lower
if user_input_lower in main_colors:
print("Es un color primarion")
else:
print("No es un color primarion")
Thanks in advance!
Here is the error in your code:
You have written: user_input_lower = user_input.lower
The correct syntax is user_input_lower = user_input.lower()
lower()
is a method / function that you are performing on the string:: user_input
. When you write .lower
it is trying to look for a class variable lower in the class definition which does not exist and hence you get the error.
First of all your indentation is wrong you need to indent your whole code after the while condition, then you have to use the parenthesis after "lower" user_input.lower()
. That was your only mistakes.