当我在python中编写一个简单的程序时,它为我创建了一个问题!我有loop
并希望将函数的工作作为循环计数运行。但我得到了用户的循环号码。我想知道以下哪种方法更好,为什么?
第一种方法:
def game(redCard, blackCard):
global totalRed, totalBlack
redWin = False
if redCard == king and blackCard == king:
redWin = True
elif redCard != king and blackCard != king and redCard != blackCard:
redWin = True
if(redWin):
totalRed += 1
else:
totalBlack += 1
def main():
#start point of program
n = int(input("Please enter 'Rounds' numbers:"))
for i in range(n):
# randint(a,b): Return a random integer N such that a <= N <= b
redCard = random.randint(0, 3)
blackCard = random.randint(0, 3)
game(redCard, blackCard)
main()
第二种方法:
def game(count):
global totalRed, totalBlack
for i in range(count):
# randint(a,b): Return a random integer N such that a <= N <= b
redCard = random.randint(0, 3)
blackCard = random.randint(0, 3)
redWin = False
if redCard == king and blackCard == king:
redWin = True
elif redCard != king and blackCard != king and redCard != blackCard:
redWin = True
if(redWin):
totalRed += 1
else:
totalBlack += 1
def main():
#start point of program
n = int(input("Please enter 'Rounds' numbers:"))
game(n)