Python - 字符串和 .format() 问题

Python - issue with strings and .format()

我刚开始学习Python,我正在制作一个小游戏来练习我所学的知识。不过,我遇到了一个小问题:

这是不起作用的部分:

name = raw_input("Enter Name: ")

print""

print "^^^^^^^^^^^^^^^^^^"

start_message = raw_input("Another day on Uranus, {name}!. Shall we go outside or stay within the protection bubble? (Press 'Enter')").format(name=name)

输出:

Enter Name: James


^^^^^^^^^^^^^^^^^^
Another day on Uranus, {name}!. Shall we go outside or stay within the protection bubble? (Press 'Enter')

您需要格式化传递给 raw_input:

的字符串
 ......(Press 'Enter')".format(name=name)

您正在尝试格式化从 raw_input 中获取的输入。

您正在格式化 raw_input 而不是 raw_input 中的字符串。您需要更改括号所在的位置:

start_message = raw_input("Another day on Uranus, {name}!. Shall we go outside or stay within the protection bubble? (Press 'Enter')".format(name=name))

请注意,我将括号从 " 结束后拉到 format 之后,现在全部在 raw_input()

输出:

Enter Name: Andy

^^^^^^^^^^^^^^^^^^
Another day on Uranus, Andy!. Shall we go outside or stay within the protection bubble? (Press 'Enter')

啊你括号错误。

start_message = raw_input("Another day on Uranus, {name}!. Shall we go outside or stay within the protection bubble? (Press 'Enter')").format(name=name)

需要

start_message = raw_input("Another day on Uranus, {name}!. Shall we go outside or stay within the protection bubble? (Press 'Enter')".format(name=name))