【问题标题】:getting Python 3.9 to recognize parameters and act accordingly (Interactive storytelling)让 Python 3.9 识别参数并采取相应措施(交互式讲故事)
【发布时间】: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


【解决方案1】:
  1. 将您的 PostGen 函数转换为以下内容:
def PostGen(rollValue):
    if rollValue <= 5:
        print("You start as a human.")
        PostStartHum()
    elif rollValue >= 6:
        print("You start as an elf.")
        PostStartElf()
    elif rollValue >= 19:
        print("You lucked out, and can start as a demigod!")
        PostStartDemi()
  1. 将您的 NewGame 函数更改为以下内容:
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.")
        rollValue = RollD20()
        print("You rolled a " + str(rollValue) + "!")
        PostGen(rollValue)
    else:
        input("Okay, bye! Press any key to exit.")
        sys.exit()

每次调用 RollD20() 时都会生成一个新的随机数。您需要将值存储在某处并将其重用于游戏会话。

【讨论】:

    【解决方案2】:

    每次拨打RollD20,您都会获得一个随机数。因此,如果您想在多个ifs 中使用相同随机数,则需要将该值塞入另一个变量中。

    【讨论】:

      【解决方案3】:
      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.")
              result = RollD20()
              print("You rolled a " + str(result) + "!")
              PostGen(result)
          else:
              input("Okay, bye! Press any key to exit.")
              sys.exit()
      

      然后从那里更改 PostGen() 以接受结果:

         def PostGen(result):
          if result <= 5:
              print("You start as a human.")
              PostStartHum()
          elif result >= 6:
              print("You start as an elf.")
              PostStartElf()
          elif result >= 19:
              print("You lucked out, and can start as a demigod!")
              PostStartDemi()
      

      【讨论】:

        猜你喜欢
        • 2017-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-09
        • 1970-01-01
        • 2022-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多