繁体   English   中英

python中的骰子滚动游戏

[英]Dice rolling game in python

我对编码和尝试编写一些掷骰子程序/游戏完全陌生。 我希望程序询问用户想要掷多少个骰子,以及他们可以“重试”在每场比赛中掷多少次骰子。 在用户完成所有尝试后,程序会跳回到第一个问题。

所以这就是我到目前为止所得到的:

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

这有效,但我不确定从哪里开始让程序询问一名玩家每场比赛获得多少次尝试,以便如果他们对结果不满意,他们可以再次滚动。 已经尝试了一段时间来弄清楚,所以感谢任何提示!

这是一个更简单的方法-

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