gpt4 book ai didi

python - 滚动两个骰子: SyntaxError: 'return' outside function?

转载 作者:行者123 更新时间:2023-12-03 08:26:58 32 4
gpt4 key购买 nike

我正在尝试实现一个将两个骰子作为输入并计算两个值的函数。

第一个值是第一个骰子获胜的次数(在所有可能的36个选择中),第二个值是第二个骰子获胜的次数。

这是我的代码:

def count_wins(dice1, dice2):
assert len(dice1) == 6 and len(dice2) == 6
dice1_wins, dice2_wins = 0, 0

# write your code here
dice1 = input('1, 2, 3, 4, 5, 6')
dice2 = input('1, 2, 3, 4, 5, 6')
def roll_dice():
return(random.randint(1, 2, 3, 4, 5, 6),random.randint(1, 2, 3, 4, 5, 6))
for i in range(36):
dice1, dice2 = roll_dice()
if dice1 > dice2:
dice1_wins+=1
if dice2 > dice1:
dice2_wins+=1
if dice2 == dice1:
pass
return (dice1_wins, dice2_wins)

但这给了我这个错误:
Error on line 17:
return (dice1_wins, dice2_wins)
^
SyntaxError: 'return' outside function

最佳答案

我试图为您修复缩进,请尝试以下代码:

import random

def count_wins():
dice1_wins, dice2_wins = 0, 0
for i in range(0,36):
dice1, dice2 = roll_dice()
if dice1 > dice2:
dice1_wins+=1
if dice2 > dice1:
dice2_wins+=1
if dice2 == dice1:
pass
return (dice1_wins, dice2_wins)


def roll_dice():
return(random.randint(1,6),random.randint(1,6))#returns random numbers between 1 to 6.

print (count_wins())#Calls the method count_wins and prints the required output.

关于python - 滚动两个骰子: SyntaxError: 'return' outside function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49461132/

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