Fwd: [Tutor] Control flow
Kent Johnson
kent37 at tds.net
Sat Jan 29 17:23:04 CET 2005
Bob Gailer wrote:
> At 04:43 AM 1/29/2005, Liam Clarke wrote:
>
>> < erk, to the list, to the List!>
>>
>> if ( bad_weather =='y' ):
>> # ask user only if weather is bad.
>> b = input ( "Weather is really bad, still go out to jog?[y/n]" )
>> if b == 'y':
>> go_jogging()
>>
>> Anyone else notice that he's never gonna go jogging if the weather is bad?
>> Unless I've got input() wrong, it only takes integers... ?
>
>
> From the docs:
> input( [prompt])
> Equivalent to eval(raw_input(prompt)).
So, it takes more than just integers, but it won't work the way the OP expects:
>>> print input('Type something: ')
Type something: 'spam ' * 4
spam spam spam spam
>>> print input('Type something: ')
Type something: y
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 0, in ?
NameError: name 'y' is not defined
- because eval('y') looks for a variable named y
>>> print input('Type something: ')
Type something: 'y'
y
It works with the quotes - it is evaluating a string literal
raw_input() would work better.
Kent
More information about the Tutor
mailing list