Python:在函数中减去2个已定义的变量

时间:2017-11-14 22:25:29

标签: python loops while-loop

我似乎遇到了While Loops的问题......

while True:
leave=input("\n"
           "\nDo you..."
            "\n"
            "\nA: Leave the ship and go out into forrest"
            "\nB: Stay in the ship and sleep through the night"
            "\n"
            "\n: ")
if leave.lower()in ("a","leave","leave the ship"):
    leaveA=("\n"
            "\nYou slowly make your way out through the hole"
            "\nand you slide down the ship. Once you reach the"
            "\nyou start to make your way through the dense"
            "\nforrest. As you make your way through the woods"
            "\nYou start to hear faint voices from your right."
            "\n")
    for char in leaveA:  # applying to each character
        time.sleep(0.01)  # the time delay
        sys.stdout.write(char)  # adding the typing effect
        sys.stdout.flush()
    health-=injure
    injury()
    print("\n"
          "\n-----Statistics-----"
          "\n"
          "\nHealth:", health,
          "\nEnergy:", energy,
          "\nArmour:", armour,
          "\nStamina:", stamina,
          "\nStrength:", strength,
          "\nAccuracy:", accuracy)


    flee=input("\n"
               "\nThis leaves you with two choices..."
               "\n"
               "\nA: Move towards the voices, in hopes that they will help 
                you."
               "\nB: Run back to the ship to look for a weapon to defend 
                 yourself."
               "\n"
               "\n: ")

我的问题是,如果你选择A中的选项A它永远不会中断,所以循环将循环返回离开,如果我在if语句之后断开代码将永远不会到达flee输入。我试图做的是让选项A留在导致逃跑,选项B导致我后面的代码中的其他东西。任何建议都表示赞赏。

1 个答案:

答案 0 :(得分:0)

为了帮助您解决问题,请允许我向您介绍如何处理此问题或许多其他问题的一般策略:

使用“分而治之”策略:

您的系统由多个部分组成,系统无法按预期工作。你是做什么?你拿走每个部分并问自己:我可以把这部分拿走,而不破坏我试图开始工作的系统的一个功能(现在不能正常工作)吗?

如果你能用“是的,我可以把它拿走”来回答这个问题,那么就这样做。这将减少您的问题并使其更容易理解,直到您最终只有必要之前的那些部分。

适用于您手头的问题:有许多代码行,服务器在您尝试开始工作的基本功能中没有用处。

'print(“Statistics”)'声明f.ex.可以删除,在你的任务中没有任何目的让循环正常工作。

继续这个策略,你最终应该有一个更短的程序,在这个程序中你将能够一目了然地发现根本问题。