[Tutor] Question about if functions
Alan Gauld
alan.gauld at btinternet.com
Fri Aug 22 07:47:22 CEST 2014
On 22/08/14 03:43, Mimi Ou Yang wrote:
> name = input("Enter your name: )
> age = input("Enter your age: )
> age = int(age)
>
> if (name and age == jimmy and 35):
> print ("BLABLABLABLABLABLAB")
>
There are two problems here.
First You need to put quote signs around 'jimmy'
since its a literal string, not a variable.
Second, and more fundamental, Python sees your
if test like this:
if (name and age) == ('jimmy' and 35)
Which Python always sees as
True == True
which is always True.
You need to rewrite it like:
if (name == 'jimmy') and (age == 35):
The parentheses are not strictly needed,
I've just used them to show how #Python reads it,
In this specific case you can shorten it slightly
to:
if (name,age) == ('jimmy',35):
but most programmers prefer the first style since
its more flexible and maintainable (the tuple style
only works for and tests)
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