2

如果我的问题不是很具体或者标题具有误导性,我很抱歉,我一直在尝试用 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

我已经在没有引号和单引号的情况下尝试过,但都没有工作任何帮助或建议表示赞赏。

4

2 回答 2

2

Rob 是对的,而且以下代码行也没有多大意义。

我建议这样做:

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
于 2012-06-02T20:27:54.877 回答
1

Python 将“n”解释为字符串文字,这意味着它本身就是一个值,您不能将一个值分配给另一个值。左侧的标记=需要是一个变量。

我建议删除第 28 行中 n 周围的双引号。不是 python 编码器,但我会本能地这样做。

于 2012-06-02T20:18:48.590 回答