2015-08-19 2 views
-2

В приведенном ниже коде, почему он не останавливается, когда во время боя установлено значение False?Вырыв из цикла Python, без перерыва

Я знаю, что это не останавливает цикл, потому что он не дойдет до части добычи, когда во время боя установлено значение False. Здесь все время цикла:

while fighting: 
    cls() 
    print("The enemy has", opponent.HP, "HP!") 
    input() 
    if int(opponent.HP) <= 0: 
     print("Yep yep") 
     winner = True 
     fighting = False 
    elif int(ownedCreatures[activeCreature].HP) <= 0: 
     winner = False 
     fighting = False 
    showFight(opponent, activeCreature) 
    allowed = ["a", "i", "r"] 
    choice = input(">>") 

    while not choice in allowed: 
     choice = input("Try again please >>") 

    if choice.lower() == "a": 
     if previousTurn == "Not defined": 
      num = random.randint(1, ownedCreatures[activeCreature].support + opponent.support) 
      if num <= ownedCreatures[activeCreature].support: 
       attacker = "player" 
       previousTurn = "player" 
      else: 
       attacker = "opponent" 
       previousTurn = "opponent" 
     else: 
      if previousTurn == "player": 
       attacker = "opponent" 
       previousTurn = "opponent" 
      else: 
       attacker = "player" 
       previousTurn = "player" 

     attack(attacker, activeCreature, opponent) 

    #if choice.lower() == "i": 

    if choice.lower() == "r": 
     num = random.randint(1, ownedCreatures[activeCreature].support + opponent.support) 
     if num <= ownedCreatures[activeCreature].support: 
      cls() 
      print("-------------------------------------------") 
      print("You succesfully escaped this horrible fight!") 
      print("-------------------------------------------\n") 
      input("Press Enter to continue... >> ") 
      winner = "Not defined" 
      fighting = False 
     else: 
      cls() 
      print("-------------------------------------------") 
      print("Think you can run that easily?") 
      print("-------------------------------------------\n") 
      input("Press Enter to continue... >> ") 

#After the fight 
if winner == True: 
    cls() 
    loot() 
elif winner == False: 
    cls() 
    print("-------------------------------------------") 
    print("You have lost the fight!") 
    print("You lost 50 Serra!") 
    serra = serra - 50 
    if serra < 0: 
     serra = 0 
    print("-------------------------------------------\n") 
    input("Press Enter to continue... >> ") 
+0

Откуда вы знаете, что это не останавливается? Это все, что вы делаете внутри цикла while? – ozgur

+2

Не могли бы вы разместить [MCVE] (http://stackoverflow.com/help/mcve)? – TigerhawkT3

+0

Пожалуйста, напечатайте значение 'enemy.HP' –

ответ

1

У вас есть три места внутри цикла, где вы установили fighting в False и все они наступающем с if условием:

  1. int(opponent.HP) <= 0
  2. int(ownedCreatures[activeCreature].HP) <= 0
  3. num <= ownedCreatures[activeCreature].support

Первое и второе условия являются постоянными внутри цикла, поэтому, если они начинают False, изменение fighting никогда не будет доступно.

Третьего: num случайного числа больше 1 так что если ownedCreatures[activeCreature].support является 0, условие никогда не будет доступно.

Распечатайте значения условий, чтобы проверить, выполнены ли они или нет.

+0

OP говорит: «Почему он не останавливается, когда битва установлена ​​в False», поэтому я полагаю, что он уверен, что он установлен на «Ложно». – ozgur

+0

Я действительно уверен, что HP ниже или равно 0. Я проверил его с помощью отпечатков и ввода(). –

+0

И когда я ставлю «перерыв», он работает, но я не хочу использовать перерыв. –

Смежные вопросы