Search code examples
pythonloopsrandomdice

(Python) while loop is breaking my random_number


so I'm doing a project where it basically chooses a random number from 1 - 6 as a mini project.

On the most part, it works. But when it loops back up, it seems to keep on rolling the same number.

Here's a screenshot of what I mean

enter image description here

As you can see, the dice number keeps on rolling the same. Can you see what is wrong in my code?

# Useful module for selecting random numbers
import random

# Loop program back to here once user presses anything
loop = 1

#Chooses random number between 1 - 6
Random_Number = (random.choice([1,2,3,4,5,6]))

while (loop < 10):
    #Printing what the user sees
    print ("===============================")
    print ("Your random dice number is:", Random_Number)
    input("Press any key to roll again")
    print ("===============================")

    #looping back to "loop = 1"
    loop = loop + 1

Solution

  • You are generating Random_Number one time, outside of the loop. Try something like this

    while (loop < 10):
        Random_Number = (random.choice([1,2,3,4,5,6]))
        #Printing what the user sees
        print ("===============================")
        print ("Your random dice number is:", Random_Number)
        input("Press any key to roll again")
        print ("===============================")
    
        loop = loop + 1