gpt4 book ai didi

Python - 将函数传递给其他函数

转载 作者:行者123 更新时间:2023-11-30 22:29:40 25 4
gpt4 key购买 nike

我在将 crapsRoll() 传递到 gameCraps() 时遇到问题。当我定义 gameCraps() 时,出现错误:

Redefining name 'crapsRoll' from outer scope.

在打印语句中我收到一个错误,上面写着

crapsRoll has no value.

import random 

#===========================crapsRoll()===============================
def crapsRoll():
roll = random.randint(2,12)
return(roll)

#===========================gameCraps()===============================
def gameCraps(crapsRoll):
crapsRoll = crapsRoll
if (crapsRoll == 7 or crapsRoll == 11):
gameState = 1
elif (crapsRoll == 2 or crapsRoll == 3 or crapsRoll == 12):
gameState = 2
else:
gameState = 3
return(gameState)

print(gameCraps())

最佳答案

您不需要将其作为参数传递。只需从另一个函数调用它即可:

def gameCraps():
crapsRoll = crapsRoll()
if (crapsRoll == 7 or crapsRoll == 11):
gameState = 1
elif (crapsRoll == 2 or crapsRoll == 3 or crapsRoll == 12):
gameState = 2
else:
gameState = 3
return(gameState)

关于Python - 将函数传递给其他函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46273332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com