【发布时间】:2021-02-23 14:51:45
【问题描述】:
我会尽量保持简短和甜蜜。
我正在编写一个互动故事,该故事会根据 d20 骰子的“滚动”而变化。我已经想办法开始了,但我认为 Python 实际上并没有在听我给它的参数,因为它只是做任何事情。
基本上,这是应该发生的事情:
玩家同意他们想要玩游戏 -> 玩家掷骰子 -> 游戏使用随机掷出的数字来确定玩家将拥有哪个开始。
目前正在发生的事情是,一切都很顺利,直到它应该吐出玩家拥有的开始。它似乎并没有根据我的参数实际决定。例如,如果玩家掷出 5 或更少,您应该使用“人类”开始,而 6 到 18 之间的任何东西都应该使用“精灵”开始。这是昨天发生的事情:
venv\Scripts\python.exe C:/Users/drago/PycharmProjects/D20/venv/main.py
Hello! Would you like to go on an adventure? y/n >> y
Great! Roll the dice.
Press R to roll the D20.
You rolled a 15!
You start as a human.
As a human, you don't have any special characteristics except your ability to learn.
相关代码如下:
def NewGame():
inp = input("Hello! Would you like to go on an adventure? y/n >> ")
if inp == "y" or inp == "yes":
print("Great! Roll the dice.")
input("Press R to roll the D20.")
print("You rolled a " + str(RollD20()) + "!")
PostGen()
else:
input("Okay, bye! Press any key to exit.")
sys.exit()
def PostGen():
if RollD20() <= 5:
print("You start as a human.")
PostStartHum()
elif RollD20() >= 6:
print("You start as an elf.")
PostStartElf()
elif RollD20() >= 19:
print("You lucked out, and can start as a demigod!")
PostStartDemi()
def RollD20():
n = random.randint(1, 20)
return n
def PostStartHum():
print("As a human, you don't have any special characteristics except your ability to learn.")
def PostStartElf():
print("As an elf, you have a high intelligence and a deep respect for tradition.")
def PostStartDemi():
print("As a demigod, you are the hand of the gods themselves; raw power incarnated in a human form...")
print("However, even mighty decendants of gods have a weakness. Be careful."
感谢您的所有帮助。
【问题讨论】:
-
您应该在第一次运行 RollD20() 时保存它的结果,并将其传递给 PostGen。然后你可以比较 if 条件中的参数,因为现在你在每个子句上运行函数,这意味着你得到随机的结果和行为。也尝试使用 linter
-
请修正你的缩进。
标签: python python-3.x random