我很新,很抱歉,如果我的问题不是非常具体,或者标题是误导性的,我一直在尝试用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
我用引号和单引号尝试了它,但都没有工作 任何帮助或建议表示赞赏。
答案 0 :(得分:2)
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
答案 1 :(得分:1)
Python将“n”解释为字符串文字,这意味着它本身就是一个值,您无法为另一个值赋值。 =
左侧的标记需要是变量。
我建议删除第28行中n的双引号。不是python编码器,但这是我本能地做的。