[Tutor] role playing game - help needed

Peter Otten __peter__ at web.de
Tue Dec 7 14:14:57 CET 2010


Robert Sjöblom wrote:

>> Close, but remember that input() returns a string. You need numbers
>> so you need to convert strings to integers.
> 
> Actually, input() only accept integers, consider the following:
>>>> input("input: ")
> input: d
> 
> Traceback (most recent call last):
> File "<pyshell#46>", line 1, in <module>
> input("input: ")
> File "<string>", line 1, in <module>
> NameError: name 'd' is not defined

You are using Python 2.x where raw_input() was used to enter strings and 
input() behaved like

eval(raw_input())

>From the above follows that input() in 2.x accepts arbitrary Python 
expressions:

Python 2.6.4 (r264:75706, Dec  7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d = 42
>>> input()
d
42
>>> input()
d**2
1764
>>> input()
"*".join([str(d)]*5)
'42*42*42*42*42'

I think I drove the point home ;)

input() in Python 3.x on the other hand is similar to 2.x's raw_input():

Python 3.1.1+ (r311:74480, Nov  2 2009, 15:45:00)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> input()
d
'd'
>>> input()
"*".join([str(d)]*5)
'"*".join([str(d)]*5)'

Peter



More information about the Tutor mailing list