Skip to main content
 首页 » 编程设计

python之使用 python 2.7 的 Raw_input

2025年05月04日13EasonJim

这是我的功能:

def mainMenu(): 
    print " Do you want to send a message(send) or add a name to the database(add) or exit the program(exit)?" 
    answer = raw_input() 
    print answer 
    if answer is "send": 
        sendMessage() 
    elif answer is "add": 
        addName() 
    elif answer is "exit": 
        sys.exit() 
    else: 
        print "Sorry, '%s' is not a valid input. Please use one 'send', 'add', or 'exit'" %answer 

无论我在else语句中输入什么结果。我唯一能真正想到的是 raw_input() 的问题。

这是 shell 的屏幕截图。语法突出显示是因为我使用的是 sublimeREPL,它只是这样做,根本不影响代码:

我已经测试了所有调用的函数,它们单独运行良好

请您参考如下方法:

尝试用 == 替换 is。

例如:

In [1]: answer = raw_input() 
arst 
 
In [2]: answer 
Out[2]: 'arst' 
 
In [3]: answer == 'arst' 
Out[3]: True 
 
In [4]: answer is 'arst' 
Out[4]: False