I have this simple bit of code I can't get to work any advice?

a = 2
b = 4
c = input()
d = 0
d = c + 5
print(d)

Say I input a, so 2, I should get 7. But I don't. This Python 3. Using Wing IDE 101 (ver. 5) here. I get this as my error output.

Traceback (most recent call last): File "", line 1, in builtins.NameError: name 'a' is not defined

有帮助吗?

解决方案

Are you sure you are using Python 3? In python 2.x you can do it by explictly evaluating an string expression with the eval() function:

 c = eval(raw_input()) # Python 2.7
 c = eval(input()) # Python 3.x

In Python 3.x input() will convert the input in a string and won't raise that error (NameError). It will raise a TypeError instead, because you cannot concatenate str and int that way.

其他提示

you can just try c = raw_input()

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top