我是一个相当新的编程和尝试创建一个非常简单的地下城游戏。我有大部分的工作,但我有一个小问题。这是我的密码:
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”,则可以正常工作,并将其返回到前一个房间,即厨房。有什么办法可以解决这个问题吗?提前感谢您的帮助!你知道吗
让我们忽略缩进问题,这可能是您复制不正确的。你知道吗
你的控制流程一团糟。基本上让我们看看你的基本结构:
你能理解为什么,不管你在这里做的剩余逻辑发生了什么,你总是会在
continue
之后回到厨房吗?你知道吗要得到您真正想要的结构,一个选择是少一点顺序地编程。下面是一个psuedocode示例,介绍了设计游戏的一种可能方法,其中一些部分故意未指定。我提供这个是为了让你思考如何设计一个有意义的游戏。你知道吗
您可以使用dictionary,它由一个表示房间的键和一个可以去的地方列表的值组成。你知道吗
例如:
一些非常草率的示例代码,但它让我明白了我的观点。编写程序的总体思路是研究如何存储数据,例如在Python中使用字典。你知道吗
Python有很多值得研究的数据类型。你知道吗
我现在就让你去修改代码,因为你已经获得了解决问题的新视角。你知道吗
你的压痕弄乱了。你知道吗
将
first = input("You are in the kitchen. There are doors to the south (s) and east (e). ")
放在while循环之前。你知道吗相关问题 更多 >
编程相关推荐