我正在处理这段代码,我知道我必须调用该函数来测试它,但我不确定哪条线我称之为函数。我想打电话给sim_yahtzee(1,5)进行我的第一次测试,我怎么知道在所有这些代码行中调用它的位置?感谢
def sim_yahtzee(nrolls,n,sides=6):
first_roll = []
for i in range (n):
rn = random.randint(1,6)
first_roll.append(rn)
number_one = first_roll.count(1)
number_two = first_roll.count(2)
number_three = first_roll.count(3)
number_four = first_roll.count(4)
number_five = first_roll.count(5)
number_six = first_roll.count(6)
for i in range (nrolls):
if number_one > number_two and number_three and number_four and number_five and number_six:
keep = [1] * number_one
elif number_two > number_one and number_three and number_four and number_five and number_six:
keep = [2] * number_two
elif number_three > number_one and number_two and number_four and number_five and number_six:
keep = [3] * number_three
elif number_four > number_one and number_two and number_three and number_five and number_six:
keep = [4] * number_four
elif number_five > number_one and number_two and number_three and number_four and number_six:
keep = [5] * number_five
elif number_six >= number_one and number_two and number_three and number_four and number_five:
keep = [6] * number_six
elif number_five == number_four or number_three or number_two or number_one:
keep = [5] * number_five
elif number_four == number_three or number_two or number_one:
keep = [4] * number_four
elif number_three == number_two or number_one:
keep = [3] * number_three
elif number_two == number_one:
keep = [2] * number_two
reroll = []
for i in range (len(first_roll)-len(keep)):
rn = random.randint(1,6)
reroll.append(rn)
first_roll = (keep + reroll)
print (first_roll)
答案 0 :(得分:0)
在python脚本中定义函数后,总是需要调用函数。在您的示例中,您可以使用
调用它def sim_yahtzee(nrolls,n,sides=6):
first_roll = []
for i in range (n):
rn = random.randint(1,6)
first_roll.append(rn)
number_one = first_roll.count(1)
number_two = first_roll.count(2)
number_three = first_roll.count(3)
number_four = first_roll.count(4)
number_five = first_roll.count(5)
number_six = first_roll.count(6)
for i in range (nrolls):
if number_one > number_two and number_three and number_four and number_five and number_six:
keep = [1] * number_one
elif number_two > number_one and number_three and number_four and number_five and number_six:
keep = [2] * number_two
elif number_three > number_one and number_two and number_four and number_five and number_six:
keep = [3] * number_three
elif number_four > number_one and number_two and number_three and number_five and number_six:
keep = [4] * number_four
elif number_five > number_one and number_two and number_three and number_four and number_six:
keep = [5] * number_five
elif number_six >= number_one and number_two and number_three and number_four and number_five:
keep = [6] * number_six
elif number_five == number_four or number_three or number_two or number_one:
keep = [5] * number_five
elif number_four == number_three or number_two or number_one:
keep = [4] * number_four
elif number_three == number_two or number_one:
keep = [3] * number_three
elif number_two == number_one:
keep = [2] * number_two
reroll = []
for i in range (len(first_roll)-len(keep)):
rn = random.randint(1,6)
reroll.append(rn)
first_roll = (keep + reroll)
print (first_roll)
sim_yahtzee(1,5)
另外,我已经更改了缩进,我假设循环应该在函数内部。缩进级别替换python中的end语句。
答案 1 :(得分:0)
确保您的“for”循环正确缩进!
通过键入函数的名称并将其args作为自己的行,您可以在定义函数后调用该函数。为了从函数接收输出,您需要使用“return”语句来返回变量,因为在函数中初始化的任何变量将在函数完成后消失。
考虑放:
return first_roll
作为函数的最后一行,并使用
调用函数print(sim_yahtzee(6,6,sides=6))
一个函数外的第一个未缩进的行。这将打印返回的变量first_roll。