Home > Back-end >  Need help modifying my python code so that it displays a message when a dice roll is the same as the
Need help modifying my python code so that it displays a message when a dice roll is the same as the

Time:09-06

So, in my python class, I am making a program that asks how many dice you want to roll and then rolls a die (via random integer generator) that amount of times; it then adds and prints the total while also giving a message if one roll has the same value as a previous roll. Everything seems to be working; I just can't figure out how I can make the code detect when I've had the same number in a row and how to display a message for that. The prompt for the lab suggests creating a variable that keeps track of the previous roll, but I'm honestly not sure how to do that either. Here is my code so far:

def main():
    num_dice = int(input('How many dice would you like to roll? '))
    while num_dice > 12 or num_dice < 3:
        print('Sorry, the number of dice must be between 3 and 12')
        num_dice = int(input('How many dice would you like to roll? '))
    roll_dice(num_dice)



def roll_dice(num_dice):
    import random
    rolls = 0
    for dice in range(1, num_dice   1):
        print(f'Dice {dice}: {(rolls2 := random.randint(1, 6))}')
        rolls  = rolls2
    print(f'Total: {rolls}')



main()

This is what the output is supposed to look like:

How many dice do you want to roll? 8
Dice 1: 1
Dice 2: 3
Dice 3: 3 -> On a roll!
Dice 4: 3 -> On a roll!
Dice 5: 6
Dice 6: 4
Dice 7: 6
Dice 8: 3
Total: 29

I don't remember going over this in class, and any help would be appreciated!

CodePudding user response:

I tested and seems working, please let me know.

def roll_dice(num_dice):
    import random
    rolls = 0
    value = []
    for dice in range(num_dice):
        rolls2 = random.randint(1, 6)
        value.append(rolls2)
        if rolls2 == value[dice-1] and dice >= 1 :
            print(f'Dice {dice 1}: {rolls2} -> On a roll!')
        else:
            print(f'Dice {dice 1}: {rolls2}')
        rolls  = rolls2
    print(f'Total: {rolls}')

The output for 8 dices:

How many dice would you like to roll? 8
Dice 1: 2
Dice 2: 4
Dice 3: 4 -> On a roll!
Dice 4: 4 -> On a roll!
Dice 5: 4 -> On a roll!
Dice 6: 2
Dice 7: 2 -> On a roll!
Dice 8: 2 -> On a roll!
Total: 24

CodePudding user response:

simply save every roll on a list and match latest roll to previous roll using simple logic a[dice]==a[dice-1]:

Code:

def main():
    num_dice = int(input('How many dice would you like to roll? '))
    while num_dice > 12 or num_dice < 3:
        print('Sorry, the number of dice must be between 3 and 12')
        num_dice = int(input('How many dice would you like to roll? '))
    roll_dice(num_dice)



def roll_dice(num_dice):
    import random
    rolls = 0
    a=[0,]
    for dice in range(1, num_dice   1):
        {rolls2 := random.randint(1, 6)}
        a.append(rolls2)
        if a[dice]==a[dice-1]:
             print(f'Dice {dice}: {rolls2}---> on a roll')
        else:
             print(f'Dice {dice}: {rolls2}')
        rolls  = rolls2
    print(f'Total: {rolls}')


main()

Output:

How many dice would you like to roll? 8
Dice 1: 4
Dice 2: 4---> on a roll
Dice 3: 3
Dice 4: 5
Dice 5: 1
Dice 6: 3
Dice 7: 3---> on a roll
Dice 8: 2
Total: 25

WITHOUT USING LIST

def main():
    num_dice = int(input('How many dice would you like to roll? '))
    while num_dice > 12 or num_dice < 3:
        print('Sorry, the number of dice must be between 3 and 12')
        num_dice = int(input('How many dice would you like to roll? '))
    roll_dice(num_dice)



def roll_dice(num_dice):
    import random
    rolls = 0
    rolls1 =0
    for dice in range(1, num_dice   1):
        {rolls2 := random.randint(1, 6)}
        if rolls1==rolls2:
             print(f'Dice {dice}: {rolls2}---> on a roll')
        else:
             print(f'Dice {dice}: {rolls2}')
        rolls1=rolls2
        rolls  = rolls2
    print(f'Total: {rolls}')


main()

Output:

How many dice would you like to roll? 12
Dice 1: 6
Dice 2: 2
Dice 3: 6
Dice 4: 3
Dice 5: 4
Dice 6: 4---> on a roll
Dice 7: 2
Dice 8: 2---> on a roll
Dice 9: 1
Dice 10: 6
Dice 11: 5
Dice 12: 3
Total: 44
  • Related