简体   繁体   中英

Dice rolling game in python

I am completely new to coding and trying to write somewhat of a dice rolling program/game. I want the program to ask how many dice the user wants to roll, and how many times they can "retry" to roll the dice per one game. After the user has rolled all of their tries the program jumps back to the first question.

So this is what i´ve got so far:

import random
def roll(dice):
    i = 0
    while i < dice:
        roll_result = random.randint(1, 6)
        i += 1
        print("You got:", roll_result)
def main():
    rolling = True
    while rolling:
        answer = input("To throw the dice press Y if you want to stop press any key")
        if answer.lower() == "Y" or answer.lower() == "y":
            number_of_die = int(input("How many dice do you want to throw? "))
            roll(number_of_die)
        else:
            print("Thank you for playing!")
            break
main()

This works but i am not sure though where to begin to make the program ask for how many tries one player gets per game so that if they are unhappy with the result they can roll again. Been trying for a while to figure it out so appreciate any tips!

Here's a simpler approach-

from random import randint

def roll(n):
    for i in range(n):
        print(f"You got {randint(1, 6)}")

def main():
    tries = int(input("Enter the number of tries each player gets: "))
    dice = int(input("How many dice do you want to roll? "))
    for j in range(tries):
        roll(dice)
        roll_again = input("Enter 'y' to roll again or anything else to quit: ")
        if roll_again.lower() != 'y':
            break

main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM