【发布时间】:2019-10-26 17:32:14
【问题描述】:
我正在为我的大学项目而苦苦挣扎。我正在做一个基于文本的冒险游戏,但我似乎无法超越这个障碍。
我尝试调用列表以显示“您已选择...”,但我似乎无法做到这一点,我尝试按照我之前对另一个列表所做的操作,但我认为我丢失的。
import random
import time
list_intro = ["The drawbridge.", "The sewers."] # naming the 2 indexes to paths ingame.
hall_rooms = ["Left door", "Right door"]
def intro():
print("You heard at the local tavern of a abandoned castle holding many treasures.")
print("You journey for 7 days until you find yourself at the bottom of the path leading to the castle.")
print("Standing at the foot of the castles runway you see two options to enter.")
for index, path in enumerate(list_intro):
# this is the loop that prints both options as well as the index value +1 (index starts @ 0).
print(str(index + 1) + ".", path)
# print("1. To enter through the drawbridge.")
# print("2. To swim under the moat and enter the sewers.")
story = {}
def chosenpath():
path = ""
while path != "1" and path != "2": # we are including input validation, meaning only predefined values will work.
path = input(
"What path will you choose? (1 or 2): ") # the inclusion of the boolean operator "and" is also here
return int(path)
def checkpath(path): # here we are simply making a function to return a string based on the option the user chooses.
print("You have chosen", list_intro[path - 1])
return entering_path(path)
def entering_path(path):
print(f"So you've entered {list_intro[path - 1]}") # here we are simply using the user input to call a item from
# our list using index values.
if path == 1:
return """You cross the drawbridge and see a gargoyle looking straight at you,
before you have time to react you feel a slight brush across your neck, you then fall to the ground
but see your body standing, it seems your head is no longer attached to your body.
Better luck next time!"""
if path == 2: # Adding a new string here for the other path.
return """You climb over the small ledge leading into the castles underbelly,unfortunately the swim wasn't great,
& you now wreak of old sewage and damp water. After walking up some stairs you find yourself in a grand dining hall,
At the back of the hall there are two doors, one on the left and one on the right. What one do you choose?"""
print(hall_rooms)
def Dining_Hall_Rooms():
print("So you've chosen", hall_rooms[-1])
intro()
path = chosenpath()
print(checkpath(path))
我没有收到错误消息,但是当我沿着“下水道”的路径运行代码时,我得到了这个 -
You heard at the local tavern of a abandoned castle holding many treasures.
You journey for 7 days until you find yourself at the bottom of the path leading to the castle.
Standing at the foot of the castles runway you see two options to enter.
1. The drawbridge.
2. The sewers.
What path will you choose? (1 or 2): 2
You have chosen The sewers.
So you've entered The sewers.
You climb over the small ledge leading into the castles underbelly,unfortunately the swim wasn't great,
& you now wreak of old sewage and damp water. After walking up some stairs you find yourself in a grand dining hall,
At the back of the hall there are two doors, one on the left and one on the right. What one do you choose?
Process finished with exit code 0
如果我遗漏了一些非常明显的东西,我很抱歉,编码当然不是我最擅长的领域,但我真的很想改进。也为语法错误道歉。
【问题讨论】:
-
预期输出是什么?你已经描述了你在哪里,但不清楚你想要实现什么......
-
我希望它指的是基于餐厅选择的门的一组新结果,我打算把一个怪物放在一个后面,一个藏宝室放在另一个后面。这意味着一个会导致游戏“获胜”,而另一个会要求重新开始。我知道我还没有定义这些,但我稍后会实施它们。我想要发生的是,就像在第 31 行它说“你已经选择”我希望它在运行时说“所以你选择了左/右门”,但到目前为止它没有,它只打印玩家进入大厅的最后一个字符串。
-
惊喜!您的代码完全按照您的要求执行。请参阅这个 [可爱的调试站点](ericlippert.com/2014/03/05/how-to-debug-small-programs) 寻求帮助。您的整个流程仅适用于一个房间转换:您要求输入 1 或 2,调用相应的函数,然后退出程序。撇开叙述,这就是您编写的全部。
-
如果你想做不止一次的事情……使用循环?
-
另外,将故事情节和控制流分开。学习足够多的编程来构建您从一个区域到另一个区域的互联世界——您的描述只是您已经很好地跟踪的简单描述,例如“下水道”。一旦您的玩家可以在情节中移动,然后您就可以载入故事情节。