我遇到了平等问题。以下代码将嵌套列表与列表骰子进行比较。
def largeStraight(dice):
straightValues = [{1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}]
return any(value.issubset(dice) for value in straightValues)
def smallStraight(dice):
straightValues = [{1, 2, 3, 4}, {2, 3, 4, 5} , {3 ,4, 5, 6}]
return any(value.issubset(dice) for value in straightValues)
def giveResult(dice):
score = 0
if(largeStraight):
score = 40
elif(smallStraight):
score = 30
else:
score = 0
return score
dice = [1,2,3,4,1]
print(giveResult(dice))
这应该从giveResult返回值30,但是得分为40。
答案 0 :(得分:3)
您需要致电您的功能:
def giveResult(dice):
score = 0
if largeStraight(dice):
score = 40
elif smallStraight(dice):
score = 30
else:
score = 0
return score
只是引用一个函数对象意味着你的第一个if
将匹配,因为大多数Python对象在布尔上下文中被认为是真的。
您可以尽早返回,稍微简化您的功能:
def giveResult(dice):
if largeStraight(dice):
return 40
if smallStraight(dice):
return 30
return 0
答案 1 :(得分:0)
你没有在你的方法中传递任何东西:
def giveResult(dice):
score = 0
if(largeStraight(dice)):
score = 40
elif(smallStraight(dice)):
score = 30
else:
score = 0
return score