2014-03-27 95 views
0
>>> a=int(input()) 

2 

>>> b=int(input()) 

4 

>>> c=input() 

r 

>>> if c==r: 

    print(b+1) 


Traceback (most recent call last): 
    File "<pyshell#5>", line 1, in <module> 
    if c==r: 
NameError: name 'r' is not defined 
+0

'如果c ==「r'' ... –

回答

0

您必須檢查"r"而不是r。如下:

>>>if c=='r': 
>>> print(b+1) 
5 

這是因爲當你輸入「r」它是一個字符串。當你做了c==r你比較變量r。這顯然不是你想要的。因此,您使用c=='r'來比較字符串。

1

input()返回一個字符串,所以如果你想檢查是否c是字符串r,你必須添加雙引號或單引號的功能。兩者都是用來表示在Python字符串:

if c == 'r': 

if c == "r":