How do I fix my dice-simulator in Python so it prints the amount of tries its taken before landing on a 6?
Question:
I’m using Python to simulate the throw of a dice, in which the program randomises numbers between 1-6 until it lands on the number 6. Furthermore, the program is supposed to count the number of tries it has thrown the dice before landing on a 6, and print out "It took x amount of tries to get a six." in which x is the number of tries it has thrown the dice before landing on a 6.
My code so far:
import random
dice = random.randint(1,6)
n = 0
while dice == 6:
n = n + 1
print("It took", n, "tries to get a 6.")
break
For some reason, the program only prints "It took 1 try to get a 6." in the terminal and shows completely blank whenever the dice doesn’t land on a 6. How do I get it to count the amount of attempts before landing on a 6, as well as print the amount of tries in combination with the statement print("It took", n, "amount of tries to get a 6.")?
Answers:
You need to write your break
command into an if-statement.
In your current code it breaks within the first iteration.
As this is an assigment i dont want to tell you everything.
But that while loop condition won’t get you there.
Try writing a blank while loop with a break-condition, think about it.
The usual pattern for repeating an action an unknown number of times is to use a while True
loop, and then break
out of the loop when the exit condition is satisfied.
rolls = 0
while True:
die = random.randint(1, 6)
rolls += 1
if die == 6:
break
print("It took", rolls, "tries to get a 6.")
As you are only defining the dice variable once, the program does not throw the dice again.
To achieve this, you should try something like this :
import random
import time
tries = 1
while True :
dice = random.randint (1, 6)
if dice != 6 :
tries += 1
else :
print (f"It took {tries} tries to get a six.")
break
time.sleep (0.5)
(There is additional code so that the program waits for 0.5 seconds after throwing the dice each time.)
I’m using Python to simulate the throw of a dice, in which the program randomises numbers between 1-6 until it lands on the number 6. Furthermore, the program is supposed to count the number of tries it has thrown the dice before landing on a 6, and print out "It took x amount of tries to get a six." in which x is the number of tries it has thrown the dice before landing on a 6.
My code so far:
import random
dice = random.randint(1,6)
n = 0
while dice == 6:
n = n + 1
print("It took", n, "tries to get a 6.")
break
For some reason, the program only prints "It took 1 try to get a 6." in the terminal and shows completely blank whenever the dice doesn’t land on a 6. How do I get it to count the amount of attempts before landing on a 6, as well as print the amount of tries in combination with the statement print("It took", n, "amount of tries to get a 6.")?
You need to write your break
command into an if-statement.
In your current code it breaks within the first iteration.
As this is an assigment i dont want to tell you everything.
But that while loop condition won’t get you there.
Try writing a blank while loop with a break-condition, think about it.
The usual pattern for repeating an action an unknown number of times is to use a while True
loop, and then break
out of the loop when the exit condition is satisfied.
rolls = 0
while True:
die = random.randint(1, 6)
rolls += 1
if die == 6:
break
print("It took", rolls, "tries to get a 6.")
As you are only defining the dice variable once, the program does not throw the dice again.
To achieve this, you should try something like this :
import random
import time
tries = 1
while True :
dice = random.randint (1, 6)
if dice != 6 :
tries += 1
else :
print (f"It took {tries} tries to get a six.")
break
time.sleep (0.5)
(There is additional code so that the program waits for 0.5 seconds after throwing the dice each time.)