python-2.7 - 这段代码有什么问题? (Python,语法错误)

标签 python-2.7 syntax-error

我是新人,如果我的问题不太具体或者标题有误导性,我很抱歉,我一直在尝试用 Python 2.7 制作游戏。到目前为止,一切都很顺利,但有一个问题,我遇到语法错误,但我不知道如何修复它。错误说: “你的程序有一个错误: * 无法分配给文字(Text game.py,第 28 行)” 在这一行中,我尝试将 'n' 指定为 1,代码如下:

print """
---------------------------------------------
|                                           |
|     TEXT GAME: THE BEGINNING!             |
|                                           |
---------------------------------------------
"""
print "What is your name adventurer?"
adv_name = raw_input("Please enter a name: ")
print "Welcome " + adv_name + ", I am called Colosso. I am the great hero of the town Isern. \nWhile I was protecting the surrounding forests I was attacked and killed. \nNow I am nothing but a mere spirit stuck in the realm of Life. \nI am here because I swore to slay the corrupt great king Blupri. \nBlupri still lives therefore I cannot travel to the realm of Death. \nI need you to slay him and his minions so I may rest in peace"
print """Do you want to help Colosso?:
1.) Yes (Start playing)
2.) No (Quit)
"""
dside = input("Please enter a number to decide: ")
if dside == 2:
    print "I knew you were a coward..."
    raw_input('Press Enter to exit')
elif dside == 1:
    print "Great! Let's get this adventure started"
    raw_input('Press Enter to continue')
print """This is the tutorial level, here is where you will learn how to play.
To move the letter of a direction, n is north, e is east, s is south, w is west.
Press the '<' key to move up and the '>' key to move down.
Try it!
"""
move = raw_input('Where would you like to go?: ')
"n" = 1
"e" = 2
"s" = 3
"w" = 4
"<" = 5
">" = 6
if move == 1:
    print "You move north."
if move == 2:
    print "You move east."
if move == 3:
    print "You move south."
if move == 4:
    print "You move west."
print move

我已经尝试过不使用引号和单引号,但都不起作用 如有任何帮助或建议,我们将不胜感激。

最佳答案

Rob is right ,并且以下几行代码没有多大意义。

我的建议是:

move = raw_input('Where would you like to go?: ')

if move == 'n':
    print "You move north."
elif move == 'e':
    print "You move east."
elif move == 's':
    print "You move south."
elif move == 'w':
    print "You move west."

或者,如果您确实想出于某种原因将输入映射到数字,请考虑创建一个字典:

directions = {"n": 1, "e": 2, "s": 3, "w": 4, "<": 5, ">": 6}

然后你可以这样做:

if directions[move] == 1:
     # etc

关于python-2.7 - 这段代码有什么问题? (Python,语法错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10865598/

相关文章:

python - 为什么我在这段代码中遇到语法错误?

ruby-on-rails - 如何在Ruby中找到中间字符?

reactjs - 在 JSX 语法(React)中使用三元条件表达式时出现语法错误

ruby-on-rails - 使用 rails 设置 backbone.js 时为 "SyntaxError: unexpected }"

visual-c++ - 错误 LNK2001 : unresolved external symbol __imp__Py_InitModule4

python - 有人可以解释这条扭曲的事吗?

python - Pycharm:将文件夹标记为 'sources root' 对于子文件夹不是递归的

multithreading - 排除 “unhandled exception in thread”错误

python - 如何将 OpenCV 与 PyQt 结合起来创建一个简单的 GUI?

actionscript-3 - 第1084章