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
答案 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。