Dice rolling simulator in Python

2019-09-22 05:15发布

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!

6条回答
SAY GOODBYE
2楼-- · 2019-09-22 05:40
from random import randint
ques = input('Do you want to dice Y/N : ')

while ques.upper() == 'Y':
    print(f'your number is {randint(1,6)}')
    ques = input('Do you want to roll again !!')
else:
    print('Thank you For Playing')
查看更多
干净又极端
3楼-- · 2019-09-22 05:46

Let's walk through the process: You already know what you need to generate random numbers.

  1. import random (or you could be more specific and say from random import randint, because we only need randint in this program)
  2. As you've already said it; 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.
  3. You need to check if the user inputs Y. And you can simply do it with "Y" in input().

code version 1.

import random
repeat = True
while repeat:
    print("You rolled",random.randint(1,6))
    print("Do you want to roll again? Y/N")
    repeat = "Y" in input()

code version 1.1 (A little better)

from random import randint
repeat = True
while repeat:
    print("You rolled",randint(1,6))
    print("Do you want to roll again?")
    repeat = ("y" or "yes") in input().lower()

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 of import random, I don't need to say random.randint(1, 6) and simply radint(1,6) will do the job.

查看更多
做自己的国王
4楼-- · 2019-09-22 05:50

This is for python 3

import random
repeat="Y"
while repeat == "Y":
   print("Rolling the dice")
   print(random.randint(1,6))

repeat =input("Do you wanna roll again Y/N?")
if repeat=="Y":
    continue
查看更多
Explosion°爆炸
5楼-- · 2019-09-22 05:51
import random
min = 1
max = 6

roll_again = "yes"
 roll_again = raw_input

while roll_again == "yes" or roll_again == "y":
    print ("Rolling the dices...")
    print ("The values are....")
    print (random.randint(min, max))
    print (random.randint(min, max))

    roll_again = raw_input("Roll the dices again?")
查看更多
地球回转人心会变
6楼-- · 2019-09-22 06:03
import random
def dice_simulate():
    number = random.randint(1,6)
    print(number)
    while(1):
       flag = str(input("Do you want to dice it up again:Enter 1 and if not enter    0"))
       if flag == '1':
         number = random.randint(1,6)
         print(number)
      else:
         print("ending the game")
         return

dice_simulate() 

For more understanding you can refre this: https://community.progress.com/code_share_group/f/169/t/35797

查看更多
等我变得足够好
7楼-- · 2019-09-22 06:05

I just started learning Python three days ago, but this is what I came up with for Python 3, and it works:

import random

question = input('Would you like to roll the dice [y/n]?\n')

while question != 'n':
    if question == 'y':
        die1 = random.randint(1, 6)
        die2 = random.randint(1, 6)
        print(die1, die2)
        question = input('Would you like to roll the dice [y/n]?\n')
    else:
        print('Invalid response. Please type "y" or "n".')
        question = input('Would you like to roll the dice [y/n]?\n')        

print('Good-bye!')
查看更多
登录 后发表回答