name = raw_input("kemal ")
quest = raw_input("To learn python ")
color = raw_input("Blue i guess ")
print "Ah, so your name is {name}, your quest is {quest}, " \
"and your favorite color is {color}.".format(name, quest, color)
我无法找到此代码的错误。 Python说" KeyError:' name' "我跑的时候。
答案 0 :(得分:1)
您需要使用命名参数在模板中使用其名称:
"...{name}....".format(name=name, quest=quest, color=color)
如果使用位置参数,则需要在模板中使用索引:
"...{0}...".format(name, quest, color)
文档:https://docs.python.org/2/library/string.html#formatstrings
答案 1 :(得分:0)
首先,您必须将字符串格式编辑为:
print "Ah, so your name is {}, your quest is {},"\
"and your favorite color is {}.".format(name, quest, color)
其次,您正在错误地使用raw_input。
您不应该输入输入内容,您应该向用户询问。所以完整正确的代码如下所示:
name = raw_input("Enter your name")
quest = raw_input("Enter your quest")
color = raw_input("Enter your color")
print "Ah, so your name is {}, your quest is {},"\
"and your favorite color is {}.".format(name, quest, color)
在提示时键入您的姓名,任务和颜色,它将正确打印出来。