Code works the long way but not as function

Question:

I have a dice program which I have programmed two different ways. One way is long, that is I have repeated code, and it works fine. When I try and put the repeated code into a function, the program no longer works when a double is rolled. This is the code that works;


while True:

    dice1 = int(random.randint(1, 6))
    dice2 = int(random.randint(1, 6))

    ask = input('Do you want to roll the dice (Y/N)? ')

    if ask == 'Y'.lower():
        print('Throws dice...')
        time.sleep(1)
        print(dice1 + dice2)

        # if player rolls a double, they get to roll again

        if dice1 == dice2:
            print('You rolled doubles!', dice1, '&', dice2, 'Roll again')
            time.sleep(0.5)
            dice1 = int(random.randint(1, 6))
            dice2 = int(random.randint(1, 6))
            print('Throws dice...')
            time.sleep(1)
            print(dice1 + dice2)

            # if the player rolls doubles a second time they get a third roll

            if dice1 == dice2:
                print('You rolled doubles for a second time!',
                      dice1, '&', dice2, 'Roll again')
                time.sleep(0.5)
                dice1 = int(random.randint(1, 6))
                dice2 = int(random.randint(1, 6))
                print('Throws dice...')
                time.sleep(1)
                print(dice1 + dice2)

As you can see, the player gets two extra rolls on doubles. In terminal this looks like this: (cannot post images yet on my profile)

*Do you want to roll the dice (Y/N)? y

Throws dice…

12

You rolled doubles! 6 & 6 Roll again

Throws dice…

6

You rolled doubles for a second time! 3 & 3 Roll again

Throws dice…

7
*

In the program with a function it looks like this;

def doubles_roll():
    time.sleep(0.5)
    dice1 = int(random.randint(1, 6))
    dice2 = int(random.randint(1, 6))
    print('Throws dice...')
    time.sleep(1)
    print(dice1 + dice2)

# while loop to maintain game as long as player indicates 'Y'


while True:

    dice1 = int(random.randint(1, 6))
    dice2 = int(random.randint(1, 6))

    ask = input('Do you want to roll the dice (Y/N)? ')

    if ask == 'Y'.lower():
        print('Throws dice...')
        time.sleep(1)
        print(dice1 + dice2)

        # if player rolls a double, they get to roll again

        if dice1 == dice2:
            print('You rolled doubles!', dice1, '&', dice2, 'Roll again')
            doubles_roll()

            # if the player rolls doubles a second time they get a third roll

            if dice1 == dice2:
                print('You rolled doubles for a second time!',
                      dice1, '&', dice2, 'Roll again')
            doubles_roll()

This code is the exact same as that above but has a function for the roll of the dice after doubles have been rolled.

The terminal output however is very different:

*Do you want to roll the dice (Y/N)? y

Throws dice…
4

You rolled doubles! 2 & 2 Roll again

Throws dice.

7

You rolled doubles for a second time! 2 & 2 Roll again

Throws dice…

9

Do you want to roll the dice (Y/N)?*

As you can see in Bold, the values of the dice never change when using a function.

Asked By: Rykorua

||

Answers:

It is because the variables dice1 and dice2 are local to the function, so whenever you compare dice1 == dice2 you are actually comparing the original roll results from lines 3 and 4. What you need to do is return the roll value from the function:

def doubles_roll():
    time.sleep(0.5)
    dice1 = int(random.randint(1, 6))
    dice2 = int(random.randint(1, 6))
    print('Throws dice...')
    time.sleep(1)
    return dice1, dice2

and then assign these values to the original variables in the main function:

dice1, dice2 = doubles_roll()
Answered By: Macko

Exactly like Macko suggested), the variable dice1 and dice2 inside the function doubles_roll() cannot be accessed outside the function where they are generated, for using them you have to return them to the function caller.

For doing so, you have to put at the end of the function doubles_roll() a return, specifying the two generated variables.

Here’s what the function should look like:

def doubles_roll():
    time.sleep(0.5)
    dice1 = int(random.randint(1, 6))
    dice2 = int(random.randint(1, 6))
    print('Throws dice...')
    time.sleep(1)
    print(dice1 + dice2)
    return dice1, dice2

while True:

    dice1 = int(random.randint(1, 6))
    dice2 = int(random.randint(1, 6))

    ask = input('Do you want to roll the dice (Y/N)? ')

    if ask == 'Y'.lower():
        print('Throws dice...')
        time.sleep(1)
        print(dice1 + dice2)

        # If a player rolls a double, they get to roll again

        if dice1 == dice2:
            print('You rolled doubles!', dice1, '&', dice2, 'Roll again')
            dice1, dice2 = doubles_roll()

            # If a player rolls doubles a second time they get a third roll

            if dice1 == dice2:
                print('You rolled doubles for a second time!',
                      dice1, '&', dice2, 'Roll again')
            dice1, dice2 = doubles_roll()
Answered By: MPizzotti
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.