문제

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