这就是我到目前为止所拥有的,它不能工作,我是Python的新手,非常抱歉,如果有一个非常明显的错误,我看不到:)
Quotes = ['Iron and Blood' , 'No Quote Available' ]
Blood=Quotes[0]
Else=Quotes[1]
Name = raw_input('Who do you want this humble AI to quote?')
if Name == Bismark:
print(Blood)发布于 2016-02-29 22:20:19
如果你想把它当做一个字符串来处理,你需要用双引号把它括起来。这是因为raw_input函数将用户输入的文本作为字符串返回,以便与之进行比较。这应该能起到作用:
Quotes = ['Iron and Blood' , 'No Quote Available' ]
Blood=Quotes[0]
Else=Quotes[1]
Name = raw_input('Who do you want this humble AI to quote?')
if Name == 'Bismark':
print(Blood)也就是说,有一些更好的或者更“pythonic”的方法可以做到这一点。这个使用字典来存储引文:
quotes = {
'Bismark': 'Iron and Blood',
'pep8': 'Variable names should be lower case to make them distinguishable from class names.',
}
not_found = 'No Quote Available'
# this would be just input for python3
name = raw_input('Who do you want this humble AI to quote?')
try:
print(quotes[name])
except KeyError:
print(not_found)这允许您向字典中添加新的引号,而无需每次都添加if语句。
https://stackoverflow.com/questions/35701199
复制相似问题