Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么当变量变为0时while循环不停止?(Python)_Python_Loops_While Loop - Fatal编程技术网

为什么当变量变为0时while循环不停止?(Python)

为什么当变量变为0时while循环不停止?(Python),python,loops,while-loop,Python,Loops,While Loop,我有一个变量叫做“在while循环中生存”。我希望它在变为0时停止函数。这不会发生,当我打印变量时,它会说值是负数,并且不会停止循环。它还有另一个条件,那就是球员。当我去掉这个条件时,它就起作用了。这真令人困惑。有人能给我解释一下吗?(请记住,我不是很有经验,我刚刚开始学习python) 首先,删除这些全局变量并在函数中使用参数。其次,您需要在while循环中使用和而不是或 import random number_of_players = 10 number_of_lives = 5 def

我有一个变量叫做“在while循环中生存”。我希望它在变为0时停止函数。这不会发生,当我打印变量时,它会说值是负数,并且不会停止循环。它还有另一个条件,那就是球员。当我去掉这个条件时,它就起作用了。这真令人困惑。有人能给我解释一下吗?(请记住,我不是很有经验,我刚刚开始学习python)


首先,删除这些全局变量并在函数中使用参数。其次,您需要在while循环中使用
而不是

import random
number_of_players = 10
number_of_lives = 5

def getRandom(lives, players):
    print("There are 9 people around you. You and the computer will both guess a number from 1 to 5. If your numbers are the same, that amount of players will be out, but if your answers are different, you lose a life. You have 5 lives and you get an extra one if you both guess the same number. Good Luck!")
    while(lives > 0 and players > 0):
          myGuess = input("What number do you choose")
          compGuess = random.randint(1,5)
          myGuess = int(myGuess)
          print(f"Computer chose {compGuess}")
          if compGuess == myGuess:
              players = players - compGuess
              if myGuess == 1:
                  print(f"You took out {compGuess} player")
              else:
                print(f"You took out {compGuess} players")
          else:
              lives -= 1
              print(f"You lost a life! You now have {lives} lives remaining")
              print(f"There are {players} players left")
              
getRandom(number_of_lives, number_of_players)

使用
时,只要至少有一个条件为真,则整个条件为真。使用
时,这两个条件都必须为真。
import random
number_of_players = 10
number_of_lives = 5

def getRandom(lives, players):
    print("There are 9 people around you. You and the computer will both guess a number from 1 to 5. If your numbers are the same, that amount of players will be out, but if your answers are different, you lose a life. You have 5 lives and you get an extra one if you both guess the same number. Good Luck!")
    while(lives > 0 and players > 0):
          myGuess = input("What number do you choose")
          compGuess = random.randint(1,5)
          myGuess = int(myGuess)
          print(f"Computer chose {compGuess}")
          if compGuess == myGuess:
              players = players - compGuess
              if myGuess == 1:
                  print(f"You took out {compGuess} player")
              else:
                print(f"You took out {compGuess} players")
          else:
              lives -= 1
              print(f"You lost a life! You now have {lives} lives remaining")
              print(f"There are {players} players left")
              
getRandom(number_of_lives, number_of_players)