Simulator voor het rollen van dobbelstenen in Python

Ik wil een programma schrijven dat een dobbelsteen gooit. Dit is wat ik heb:

import random
print("You rolled",random.randint(1,6))

En ik wil ook zoiets als dit kunnen doen:

print("Do you want to roll again? Y/N")

en als ik dan op Y druk, rolt het opnieuw en als ik op N druk, sluit ik de app af. Bij voorbaat dank!


Antwoord 1, autoriteit 100%

Laten we het proces eens doornemen:
Je weet al wat je nodig hebt om willekeurige getallen te genereren.

  1. import random(of je kunt specifieker zijn en zeggen from random import randint, omdat we alleen randintnodig hebben in dit programma)
  2. Zoals je het al zei; print("You rolled",random.randint(1,6))“gooit de dobbelstenen”.
    maar het doet het maar één keer, dus je hebt een lus nodig om het te herhalen. Een while-lusroept naar ons.
  3. Je moet controleren of de gebruiker Yinvoert. En je kunt het gewoon doen met "Y" in input().

codeversie 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 versie 1.1 (een beetje beter)

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 deze code is de gebruiker gratis om snaren te gebruiken zoals yEs, y, yEs, yEsen … om door te gaan met de lus.

Onthoud nu, in versie 1.1, sinds ik gebruikte from random import randintin plaats van import random, ik hoef niet te zeggen random.randint(1, 6)en eenvoudig radint(1,6)Wordt de taak.


Antwoord 2

Dit is voor 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

Antwoord 3

Ik ben net begonnen met het leren van Python drie dagen geleden, maar dit kwam ik voor Python 3, en het werkt:

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

Antwoord 4

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

Antwoord 5

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

Antwoord 6

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() 

Voor meer begrip kun je dit raadplegen: https://community.progress .com/code_share_group/f/169/t/35797

Other episodes