原始输入无法正常工作 - 我错过了一个模块吗?

时间:2016-03-07 12:51:47

标签: python

我非常担心这个非常基本的问题! 我是一个Python新手,刚开始。我已经尝试了一些方法来让以下工作,现在我想知道是否有一个模块我需要导入以使raw_input工作?我在Python 2.7.10上。 我想打印"简很棒"如果用户输入是Jane,并且对于所有其他用户输入,则打印名称+"不是很棒"。但它总是打印名称+"即使我进入Jane,也不会很棒#34;

1)

while True:
    name = raw_input("What is your name?")
    if name == "Jane":
        print name + " is awesome"
        break
    else:
        print name + " is not awesome"

2)

endprogram = 0
while endprogram != 1:
    name = raw_input ("What is your name?")
    while name != "Jane":
        print name + " is not awesome"
        name = raw_input ("What is your name?")
    print "Jane is awesome!"
    endprogram = 1

1 个答案:

答案 0 :(得分:3)

这两段代码都运行得很好。输入Jane,它就会爆发:

>>> while True:
...     name = raw_input("What is your name?")
...     if name == "Jane":
...         print name + " is awesome"
...         break
...     else:
...         print name + " is not awesome"
...
What is your name?Martijn
Martijn is not awesome
What is your name?Jane
Jane is awesome

添加repr() function call以帮助调试您可能遇到的任何问题,例如额外的空格或缺少大小写或ASCII以外的特殊字符:

>>> while True:
...     name = raw_input("What is your name?")
...     print 'You entered:', repr(name)
...     if name == "Jane":
...         print name + " is awesome"
...         break
...     else:
...         print name + " is not awesome"
...
What is your name?Martijn
You entered: 'Martijn'
Martijn is not awesome
What is your name? Jane
You entered: ' Jane'
 Jane is not awesome
What is your name?jané
You entered: 'jan\xc3\xa9'
jané is not awesome
What is your name?Jane
You entered: 'Jane'
Jane is awesome

请注意我第一次“尝试”输入Jane时的初始空格(值周围的引号显示那里有空格)。输入é时,我的终端向Python发送UTF-8字节,显示为两个转义值,十六进制C3和A9。