0

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

asked Mar 11, 2014 at 17:24

2 Answers 2

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.

answered Mar 11, 2014 at 17:29
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, so I have done that but it spits out this error in the terminal: Enter the country: uk Traceback (most recent call last): File "<stdin>", line 4, in <module> File "<string>", line 1, in <module> NameError: name 'uk' is not defined
@user3407221 - You must be using Python 2.x. Use raw_input to get the user input instead of input. The former will return a string object where as the latter will try to evaluate its input.
0

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:
answered Mar 11, 2014 at 17:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.