简体   繁体   English

python中的骰子滚动游戏

[英]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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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