I want to write a program that rolls a dice. Now this is what I have :
import random
print("You rolled",random.randint(1,6))
And I also want to be able to do something like this:
print("Do you want to roll again? Y/N")
and then if I press Y it rolls again and if I press N I quit the app. Thanks in advance!
Let's walk through the process: You already know what you need to generate random numbers.
import random
(or you could be more specific and sayfrom random import randint
, because we only needrandint
in this program)print("You rolled",random.randint(1,6))
"rolls the dice". but it does it only once, so you need a loop to repeat it. A while loop is calling to us.Y
. And you can simply do it with"Y" in input()
.code version 1.
code version 1.1 (A little better)
In this code, the user is free to use strings like
yEs
,y
,yes
,YES
and ... to continue the loop.Now remember, in version 1.1, since I used
from random import randint
instead ofimport random
, I don't need to sayrandom.randint(1, 6)
and simplyradint(1,6)
will do the job.This is for python 3
For more understanding you can refre this: https://community.progress.com/code_share_group/f/169/t/35797
I just started learning Python three days ago, but this is what I came up with for Python 3, and it works: