我如何返回到更高的一行,并让它在Python中执行?
我对编程相当陌生,正在尝试创建一个非常简单的地下城游戏。我大部分时间都在工作,但我有一个小问题。这是我的密码:我如何返回到更高的一行,并让它在Python中执行?,python,Python,我对编程相当陌生,正在尝试创建一个非常简单的地下城游戏。我大部分时间都在工作,但我有一个小问题。这是我的密码: print("Welcome to Matt's Dungeon!") user = "" stop = "q" while user != "q": first = input("You are in the kitchen. There are doors to the south (s) and east (e). ") if first == "s": print(
print("Welcome to Matt's Dungeon!")
user = ""
stop = "q"
while user != "q":
first = input("You are in the kitchen. There are doors to the south (s) and east (e). ")
if first == "s":
print("You entered the furnace and fry yourself to death!")
break
elif first == "q":
break
elif first == "e":
second = input("You are in the hallway. There are doors to the west (w), south (s), and east (e). ")
if second == "w":
first == "s"
elif second == "q":
break
elif second == "e":
print("You are in the library. You found the princess! You are a hero!")
break
elif second == "s":
third = input("You are in the living room. There are doors to the west (w) and north (n). ")
if third == "w":
print("You entered the furnace and fry yourself to death!")
break
elif third == "n":
first == "e"
elif third == "q":
break
print("Goodbye!")
我遇到的问题是,如果用户在客厅输入“n”,我希望它返回到走廊,但程序总是将其发送回原始厨房。但是,如果用户在走廊中输入“w”,则可以正常工作,并将其返回到前一个房间,即厨房。有没有办法解决这个问题?提前感谢您的帮助 您可以使用由表示房间的钥匙和您可以去的地方列表的值组成的
例如:
# these match up to indexes for the list in the dict directions
NORTH = 0
EAST = 1
WEST = 2
SOUTH = 3
directions = {
"living room": ["dining room", None, None, "bedroom"]
}
# the current room, represented by the keys you create
current_room = "living room"
# an example imput
direction = "n"
if direction == "n":
possible_room = directions[current_room][NORTH]
if possible_room:
current_room = possible_room
一些非常草率的示例代码,但它让我明白了我的观点。编写一个程序的总体思路是研究如何存储数据,比如在Python中使用字典
Python有很多值得研究的数据类型
我将让您现在修复代码,因为您已经获得了解决问题的新视角。您的缩进被弄乱了
在while循环之前放置
first=input(“你在厨房里。南(s)和东(e)有门)。
。让我们忽略缩进问题,这些问题可能是你复制错误的
你的控制流是一团混乱。基本上,让我们看看您的基本结构:
while True:
first = input("You are in kitchen")
# additional program logic
你知道为什么吗,不管你在这里做的剩余逻辑发生了什么,你总是会在继续后回到厨房
获得您真正想要的结构的一个选项是编程的顺序稍微少一点。下面是一个psuedocode示例,介绍了一种可能的游戏设计方法,其中一些设计部分故意未指定。我提供这篇文章是为了让你思考如何以合理的方式设计游戏
class Room():
def __init__(self,north,south,east,west):
self.north=north
self.south=south
self.east=east
self.west=west
kitchen = Rooms(None, 'hallway', 'library', None)
#initialization of other rooms are left as excercise to the reader
current_room = kitchen
while True:
print "You are in the %s" % current_room
move=raw_input("Where do you want to go")
if move=='q':
print "bye"
if move=='e':
current_room = current_room.east
#much logic is left as an exercise to the reader
你的缩进不正确。