ChalzZy
ChalzZy

Reputation: 25

(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

Upvotes: 1

Views: 1248

Answers (6)

Laurentourte
Laurentourte

Reputation: 89

If you don't want to redefine a random number at every iteration:

# 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 = lambda : 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())
    print ("===============================")

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

Upvotes: 0

alexis
alexis

Reputation: 50220

You need to understand that Python (and similar languages) stores values, not expressions. If you write a = 2 + 2, there is no addition and no 2 in the variable a; there's just the number 4.

Your situation is exactly the same: You thought you defined Random_Number as an alias for the expression next to it, where in reality you only store a number.

You can of course fix the problem by calling random.choice() inside the loop-- as about 10 answers have already suggested. But to do what you meant to do, define a function that selects a number in the way you specified. Function bodies are executed every time you call the function.

def random_number():
    return random.choice([1,2,3,4,5,6])

while (loop < 10):
    print("you rolled", random_number())
    loop += 1

Upvotes: 1

Łukasz Szczesiak
Łukasz Szczesiak

Reputation: 214

Because you generate random number only once. It should be

...
while (loop < 10):
    Random_Number = (random.choice([1,2,3,4,5,6]))
....

Also dont name variables with upper letters instead Random_Number use random_number

Upvotes: 0

Alex Pshenko
Alex Pshenko

Reputation: 167

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

Upvotes: 2

user3556757
user3556757

Reputation: 3619

You set the value of Random_Number only once, and then show that on every loop.

Fixed

# 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.choice([1,2,3,4,5,6])))
    #input("Press any key to roll again")
    print ("===============================")

    #looping back to "loop = 1"

Upvotes: 0

Mureinik
Mureinik

Reputation: 311823

This code chooses the random number once, and then just prints it 10 times. If you want a different random number each time, you should move the random selection inside the loop:

while (loop < 10):
    #Chooses random number between 1 - 6
    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 ("===============================")

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

Upvotes: 1

Related Questions