嘿,我是 Python 的新手,我正在尝试使用一个函数调用另一个函数来模拟掷骰子游戏。不幸的是,每当我测试我的代码时,我总是要么从所有游戏中获得 100% 的胜利,要么从所有游戏中获得 100% 的失败。我知道游戏,我以前称之为掷骰子游戏的变量,只触发一次并使用同一个游戏来计算它。问题是我如何再次启动变量游戏。这是我的代码:
def testcraps(n):
losses=0
wins=0
game=quietCraps()
for x in range(n):
game
print('quietcraps:')
print(game)
if game==1:
wins=wins+1
if game==0:
losses=losses+1
Total=wins/n
print('wins:')
print(wins)
print('Total:')
print(Total)
def quietCraps():
import random
dice=random.randrange(2,12)
dice2=random.randrange(2,12)
if dice==7 or dice==11:
return 1
elif dice==2 or dice==3 or dice==12:
return 0
else:
while dice2!=dice or dice2!=7:
dice2=random.randrange(2,12)
if dice2==dice:
return 1
if dice2==7:
return 0
最佳答案
你只调用 quietCraps()
一次,在循环外:
game=quietCraps()
for x in range(n):
game
game
表达式什么都不做;没有进行新的 quietCraps()
调用。如果您想为每个循环调用一个新游戏,请执行以下操作:
for x in range(n):
game=quietCraps()
关于Python用另一个函数调用一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17088247/