-1

I am trying to write the program such that player 1 rolls the dice twice, as does player 2. The summed outcome of the dice roll of player 1 is measured against that of player 2. If the summed outcome for player 1 is the same as player 2, then the dice rolling has to occur again in the same way it did initially. The output will read as to which player rolled the highest combined number.

import random

print('Player 1, pls roll the dice twice:')

MIN = 1
MAX = 6


def main():

    dice = dice_outcome()

    if r3 > r4:
        print('Player 1 goes first')
    elif r4 > r3:
        print('Player 2 goes first')
    else:
        print('As both numbers are the same we need to randomly select again')
        return dice_outcome()

def dice_outcome():

    r1 = random.randint(MIN, MAX)
    r2 = random.randint(MIN, MAX)
    print(r1)
    print(r2)
    r3 = r1 + r2
    print('The total of the two dice rolls is', r3)
    print('')
    return r3

    print('Player 2, pls roll the dice twice: ')

    r1 = random.randint(MIN, MAX)
    r2 = random.randint(MIN, MAX)
    print(r1)
    print(r2)
    r4 = r1 + r2
    print('The total of the two dice rolls is', r4)
    return r4

main()
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Paul T
  • 25
  • 5
  • You unconditionally `return r3`. Code in that function after that line won't ever execute. Also, variables defined in one function can't be accessed in another function. `r3` and `r4` aren't available in `main`. – Mark Tolonen Oct 29 '20 at 18:45
  • https://stackoverflow.com/questions/354883/how-do-i-return-multiple-values-from-a-function – Kenny Ostrom Oct 29 '20 at 18:47

2 Answers2

0

Well first of all you can't return twice in a function. You also need to include r3 and r4 in your global scope(at the beginning of your programm) for it to work, as you can't currently get the values of r3 and r4 outside the function get_outcome(). If you want to do this using functional programming I would compare r3 and r4 in the function get_outcome() and return the number 1 if the first player rolled higher or the number 2 if the second player rolled higher:

import random

def dice_outcome():

  global r3
  global r4

  r1 = random.randint(MIN, MAX)
  r2 = random.randint(MIN, MAX)
  print(r1)
  print(r2)
  r3 = r1 + r2
  print('The total of the two dice rolls is', r3)
  print('')

  print('Player 2, pls roll the dice twice: ')

  r1 = random.randint(MIN, MAX)
  r2 = random.randint(MIN, MAX)
  print(r1)
  print(r2)
  r4 = r1 + r2
  print('The total of the two dice rolls is', r4)



print('Player 1, pls roll the dice twice:')

MIN = 1 
MAX = 6

dice_outcome()

if r3 > r4:
    print('Player 1 goes first')
elif r4 > r3:
    print('Player 2 goes first')
else:
    print('As both numbers are the same we need to randomly select again')
oberhuber
  • 11
  • 3
  • You can accept an answer by clicking the little checkmark, as to show people who search for this in the future that this is the right answer. This also gives me points. By clicking the up arrow you will give points too, this would be much appreciated. – oberhuber Nov 02 '20 at 17:53
0

If I interpret correctly, this is the code you're looking for


import random

print('Player 1, pls roll the dice twice:')

MIN = 1 
MAX = 6


def dice_outcome():

    r1 = random.randint(MIN, MAX)
    r2 = random.randint(MIN, MAX)
    print(r1)
    print(r2)
    r3 = r1 + r2
    print('The total of the two dice rolls is', r3)
    print('')
    
    print('Player 2, pls roll the dice twice: ')
    
    r1 = random.randint(MIN, MAX)
    r2 = random.randint(MIN, MAX)
    print(r1)
    print(r2)
    r4 = r1 + r2
    print('The total of the two dice rolls is', r4)
    return r3, r4
    
def main():
    
    end = False
    
    while(not end):
        r3, r4 = dice_outcome()
        if r3 > r4:
            print('Player 1 goes first')
            end=True
        elif r4 > r3:
            print('Player 2 goes first')
            end=True
        else:
            print('As both numbers are the same we need to randomly select again')
    
main()