[Tutor] Python Programming

Alan Gauld alan.gauld at btinternet.com
Wed Aug 27 20:57:12 CEST 2014


On 27/08/14 14:40, Jake wrote:
> To whom it may concern,
> My name is Jake and I have recently started the GCSE computing course with school.

> answera = input()
> if answera == ["Oslo" or "oslo"]:

This doesn't do what you think it does.

["Oslo" or "oslo"]  is a list

"Oslo" or "oslo"   is the content of the list and
is a boolean expression which evaluates to True.
(Each non-empty string is considered True by Python)

So your 'if' line looks to Python like:

if answera == [True]:

But answera is a string so it will never equal a list
with a single boolean value so you go to the else
clause.

A better way to do what you want is to convert the input
to lowercase using the string lower() method and compare
that to the string you want, like so:

if answera.lower() == "oslo":

If you need to check multiple possible answers you can use
a list of strings and the 'in' operator like this:

if answera.lower() in ['amsterdam', 'london', 'oslo']:

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list