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!