gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:00:42 25 4
gpt4 key购买 nike

如果我的问题不是很具体或者标题具有误导性,我很抱歉,我一直在尝试用 Python 2.7 制作游戏。到目前为止,一切都很顺利,但有一个问题,我收到一个语法错误,我不知道如何修复它。错误说:
“你的程序有错误:
* 不能分配给文字(文本 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com