Python 3.x 我在while循环中有一个if函数,但它要么忽略它,要么不';不要打断

Python 3.x 我在while循环中有一个if函数,但它要么忽略它,要么不';不要打断,python-3.x,if-statement,while-loop,pycharm,break,Python 3.x,If Statement,While Loop,Pycharm,Break,抱歉,如果格式不正确,我是stackoverflow新手。我正在为我的英语项目编写一个文字游戏。我试图在“while”循环中运行这些不同的“if”函数,但它要么忽略它们,要么只是循环整个函数。如果您选择“直”,那么它应该拉一个随机数生成器并选择一个随机场景。如果你选择“左”,它只会减少你的食物。“左”功能有效,但“直”功能无效。另外,我正在尝试让它在通过“left”函数或“direct”函数运行后中断,但由于“if”函数,我不确定如何中断。这是在Pycharm上编程的,我相信它是python 3

抱歉,如果格式不正确,我是stackoverflow新手。我正在为我的英语项目编写一个文字游戏。我试图在“while”循环中运行这些不同的“if”函数,但它要么忽略它们,要么只是循环整个函数。如果您选择“直”,那么它应该拉一个随机数生成器并选择一个随机场景。如果你选择“左”,它只会减少你的食物。“左”功能有效,但“直”功能无效。另外,我正在尝试让它在通过“left”函数或“direct”函数运行后中断,但由于“if”函数,我不确定如何中断。这是在Pycharm上编程的,我相信它是python 3.7.0。

您的
random.randint(1,5)
行没有赋值给变量,因此没有任何if语句被触发。试着做类似的事情

while True:
    print("We have entered in the open sea, but the current is pushing us. 
            We can veer left and"
              +"\nmiss the storm, or go straight through it. Type [left] or 
              [straight] exactly as it is in the box.")
    turnChoice = input()

    if turnChoice == "left":
        food = food - 2
        time.sleep(2)
        print("You missed the storm, but you lost a little food. Your 
            current 
          food level is 5. If you let it go to zero, "
          +"\nyour health will decrease.")
        break
    if turnChoice == "straight":
        time.sleep(2)
        random.randint(1,5)
        break
    if random == 1:
        food = food - 2
        print("You tried to go through the storm, but the ship was damaged. 
            You lost some food.")
        break
    if random == 2:
        print("The ship made it through the storm without taking damage 
                somehow. 
            You did not loose any supplies or health.")
        break
    if random == 3:
        print("The ship made it through. It made a lot of noise, but no 
            supplies were lost.")
        break
    if random == 4:
        health = health - 20
        print("The ship made it through, but there was damage. While you 
            were inside the ship, a stack of barrels fell on you."
            +"\n You took some damage, receiving a concussion and fracturing 
            your arm. But you will live.")
        break
    if random == 5:
        water = water - 2
        print("You made it through the storm, but some water barrels were 
            lost to the storm. ")
        time.sleep(2)
        print("Here is your water status, don't let it get to zero: ")
        print(water)
        break

    if turnChoice not in("left", "straight"):
        print("Invalid input, please try again.")
两件事:

  • Break用于退出while循环。对于python,if语句将根据缩进退出。因此,对于直跑和继续您的场景,您只需在直跑之后删除中断

  • random.randint将为您获取一个介于1和5之间(含1和5)的随机整数,但不会将其保存在用于检查场景的变量中。所以你需要随机存储它。“random.”部分只是从您导入的random模块中获取此函数

  • 这将退出if语句,而不是while循环,当它们键入stright并正确分配random时。这两个变化为我解决了问题

    rnd = random.randint(1,5)
    
    if rnd == 1:
        # stuff here
    if rnd == 2:
        # stuff here
    

    在我的例子中,它适用于左和直。当你选择“直选”时,它选择了什么?我将试着通过问一个问题来帮助你。
    break
    做什么?
    if turnChoice == "straight":
        time.sleep(2)
        random = random.randint(1,5)
    if random == 1: #rest of code