+ 1
Finding out the lowest number when using the random number.
I'm trying to work out how to find the lowest number when a dice is thrown x amount of times. My program only shows me the last number thrown. Any help greatly appreciated. import random x = 0 while (x < 6): dice_roll = str (random.randint(1,6)) print("you rolled a " + dice_roll) x = x + 1 print ("the lowest number rolled was " + min (dice_roll))
3 Answers
+ 3
Practically what was said above but if the language that's being used is let's say Python then you could make a list which will keep all of the entries. Then you could use the min function to return the lowest value.
import random
x = 0
a = []
while (x < 6):
dice_roll = str (random.randint(1,6))
print("you rolled a " + dice_roll)
a.append(dice_roll)
x = x + 1
print ("the lowest number rolled was " + min (a))
+ 2
Hello Brian.
You are always overwriting the variable dice_roll, thats why it shows the last number.
I don't know what Language is this, but you can do something like this:
x = 0
lowest = 0
while (x < 6):
dice_roll = str (random.randint(1,6))
print("you rolled a " + dice_roll)
if (lowest == 0 || dice_roll < lowest):
lowest = dice_roll
x = x + 1
print ("the lowest number rolled was " + lowest)
Another way to do this, is using dice_roll as an Array
Like this:
x = 0
while (x < 6):
dice_roll[x] = str (random.randint(1,6))
print("you rolled a " + dice_roll[x])
x = x + 1
print ("the lowest number rolled was " + min (dice_roll))
I'm not sure if the syntax is correct, but that is the idea.
Hope it helps.
0
Thank you very much. I will try this.