So I've been learning the basics of Python and threw this bit of code together but i'm having a few issues. So I basically want it to check if the user input matches a value in the 'con' variable and if it matches to print correct, if it's wrong print not recognised.
#countries.py
con = [ "uk" , "japan" , "us" ]
uInput = input("Enter the country: ")
if uInput == con:
print("Correct")
else:
print("Not Recognised")
Also, I'd like to add lower() to the users input, so the capitalisation doesn't affect the results but don't really know where to add it.
Like I say, I'm new to coding so go easy!
Cheers
2 Answers 2
con is a list and uInput is a string. Meaning, they will never be equal.
Instead, you want to use in here:
if uInput in con:
The above code will test if the value of uInput can be found in con.
Then, you can add str.lower for case-insensitive searching:
if uInput.lower() in con:
This code will test if a lowercase version of the value of uInput can be found in con.
2 Comments
You can chain that method straight onto the string that input() returns:
uInput = input("Enter the country: ").lower()
Note also that the input will never be == con, as con is a list and the input is a string; instead, try:
if uInput in con: