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

时间:2015-10-02 19:31:05

标签: python raw-input string.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')

3 个答案:

答案 0 :(得分:1)

啊你有一个括号错误。

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))

答案 1 :(得分:0)

您需要格式化传递给 ......(Press 'Enter')".format(name=name)

的字符串
raw_input

您正尝试格式化您从SELECT pos FROM ( SELECT ROW_NUMBER() OVER() AS pos, h FROM (FLATTEN(( SELECT SPLIT(RPAD('', :VAR_END, '.'),'') AS h FROM (SELECT NULL)),h ))) WHERE pos BETWEEN :VAR_START AND :VAR_END AND (pos - :VAR_START) % :VAR_STEP = 0 获得的输入。

答案 2 :(得分:0)

您正在格式化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')