user7972041
user7972041

Reputation:

In Python, how do I ask the user to input a Y or N to continue?

I want to write a program that rolls a die. 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!

Upvotes: 1

Views: 127424

Answers (7)

Pierre 2001
Pierre 2001

Reputation: 141

I also found the following code:

import random as rand

print("Welcome to the Python Die Roller!")
numberOfDice = int(input("How many dice do you want to roll? ")
sidesOnADice = int(input("How many sides do you want to roll dice of? d"))
for i in range(numberOfDice):
    print(rand.randrange(start=1,
                         stop=sidesOnADice,
                         step=1))
while True:
    yesOrNo = input("Do you want to roll again? (Y)es/(N)o: ")
    if yesOrNo == "y" or yesOrNo == "Y":
        for i in range(numberOfDice):
            print(rand.randrange(start=1,
                                 stop=sidesOnADice,
                                 step=1)
    elif yesOrNo == "n" or yesOrNo == "N":
        break

The trailing d in line 5 is because of the names given to different types of dice, e.g. d6 for a hexahedral die, and d8 for an octahedral die.

Upvotes: 0

Amir Shabani
Amir Shabani

Reputation: 4227

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 randint(1,6) will do the job.

Upvotes: 14

Samiksha
Samiksha

Reputation: 11

from random import randint
ques = input('Do you want to roll dice? (Y)es/(N)o : ')

while ques.upper() == 'Y':
    print(f'your number is {randint(1,6)}')
    ques = input('Do you want to roll again? (Y)es/(N)o')
else:
    print('Thank you For Playing')

Upvotes: 0

Preeti Duhan
Preeti Duhan

Reputation: 97

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

Upvotes: 0

user9184791
user9184791

Reputation: 11

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?")

Upvotes: 0

Rick Wohlschlag
Rick Wohlschlag

Reputation: 23

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!')

Upvotes: 0

Dipin
Dipin

Reputation: 29

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

Upvotes: -1

Related Questions