[Tutor] "And" function
Brian van den Broek
bvande at po-box.mcgill.ca
Tue Jul 5 10:11:03 CEST 2005
gordnjen said unto the world upon 04/07/2005 22:24:
> I need to write a program that will do the following:
>
> Ask the user's age.
> If their age is below 1 yr old, it prints "you are old enought to eat baby
> food"
> If they are over 16, it prints "You are old enough to drive"
> If they are over 65, it prints "You are old enough to drive" and "You are
> old enough to retire"
>
> If they are between the ages of 16 and 25, it prints "you are old enough to
> get a student discount".
Hi Jennine,
> So far, I have this:
>
> age =int(raw_input("How old are you?"))
> if age>16:
> print "You are old enough to drive!"
> if age>65:
> print "You are old enough to retire!"
> if age<1:
> print "You are old enough to eat baby food!"
And this works? Is it your actual code? I ask as it seems to me as
though if age = 0.5, age > 16 will evaluate as False, and the flow
will never get to if age < 1.
> I get stuck with the old enough to get a student discount part. I have tried
>
> if age>16 and age<25:
> print "You are old enough to get a student discount"
>
> But it doesn't seem to work.
Can you define "doesn't work"? Did you get a syntax error? Or just not
the behaviour you expected?
Unless your fancy html mail (please don't do that when posting here,
by the way) stripped the indents, the problem is you didn't indent
your print statement into the if block. Witness:
>>> num = 97
>>> if num > 10 and num < 30:
... print "In the range"
...
>>> num = 20
>>> if num > 10 and num < 30:
... print "In the range"
...
In the range
>>>
Even better is:
>>> if 10 < num < 30:
... print "Still there"
...
Still there
>>>
HTH,
Brian vdB
More information about the Tutor
mailing list