Reputation: 19
I'm trying to implement a function that takes two dices as input and computes two values.
The first value is the number of times the first dice wins (out of all possible 36 choices), the second value is the number of times the second dice wins.
Here is my code:
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)
But it gives me this error:
Error on line 17:
return (dice1_wins, dice2_wins)
^
SyntaxError: 'return' outside function
Upvotes: 0
Views: 758
Reputation: 1
from random import randint, seed
from datetime import datetime
seed(datetime.now())
dice1=[2, 2, 2, 2, 3, 3]
dice2=[1, 1, 1, 1, 6, 6]
num_rounds = 36
assert len(dice1) == 6 and len(dice2) == 6
num_dice1_wins = 0
num_dice2_wins = 0
for _ in range(num_rounds):
dice1_result = dice1[randint(0, 5)]
dice2_result = dice2[randint(0, 5)]
if dice1_result > dice2_result:
num_dice1_wins += 1
elif dice2_result > dice1_result:
num_dice2_wins += 1
if num_dice1_wins > num_dice2_wins:
print(" ({} , {} ) ".format( num_dice1_wins,num_dice2_wins))
Upvotes: 0
Reputation: 1
from random import randint
dice1 = [1, 1, 6, 6, 8, 8]
dice2 = [2, 2, 4, 4, 9, 9]
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(randint(1,6),randint(1,6))#returns random numbers between 1 to 6.
print (count_wins())
Upvotes: 0
Reputation: 840
I tried to fix the indentation for you, Try the below code:
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.
Upvotes: 1