骰子滚动模拟器

问题描述 投票:-1回答:2

我正在用python制作骰子滚动模拟器来播放dnd。我是python的新手,所以如果我的代码真的很糟糕,请不要取笑我。

import random
while 1 == 1:
    dice = input("What kind of dice would you like to roll?: ") # This is asking what kind of dice to roll (d20, d12, d10, etc.)
    number = int(input("How many times?: ")) # This is the number of times that the program will roll the dice
    if dice == 'd20':
        print(random.randint(1,21) * number)
    elif dice == 'd12':
        print(random.randint(1,13) * number)
    elif dice == 'd10':
        print(random.randint(1,11) * number)
    elif dice == 'd8':
        print(random.randint(1,9) * number)
    elif dice == 'd6':
        print(random.randint(1,7) * number)
    elif dice == 'd4':
        print(random.randint(1,5) * number)
    elif dice == 'd100':
        print(random.randint(1,101) * number)
    elif dice == 'help':
        print("d100, d20, d12, d10, d8, d6, d4, help, quit")
    elif dice == 'quit':
        break
    else:
        print("That's not an option. Type help to get a list of different commands.")

quit()

我最初的注意力只是顺其自然,而不是让数字可变,但是我的弟弟提醒我,有些武器具有多次掷骰,而不是仅仅多次掷骰,我想输入一个信息询问掷骰子。现在我的代码存在的问题是,它将数字随机化,然后then乘以2。我要它做的是乘以[[different整数的数量并将其相加。

python-3.x simulator dice
2个回答
1
投票
也许使用for循环并在number上迭代用户想要掷骰子的次数,同时将这些骰子保存到列表中以显示骰子的每一卷。

例如,第一个骰子可能看起来像这样:

rolls = [] if dice == 'd20': for roll in range(number): rolls.append(random.randint(1,21)) print('rolls: ', ', '.join([str(roll) for roll in rolls])) print('total:', sum(rolls))

示例输出:

What kind of dice would you like to roll?: d20 How many times?: 2 rolls: 10, 15 total: 25


0
投票
import random print("Rolling the Dice....") dice_number = random.randint(1, 6) print(dice_number) limit = 0 while limit <= 4: ask = input("Would you like to roll again?? Yes or No ").upper() limit = limit + 1 if ask == "YES": print(random.randint(1, 6)) elif ask == "NO": print("Thank You.") break else: print("Thank You.") break else: print("Limit Reached..TRY AGAIN!!")
© www.soinside.com 2019 - 2024. All rights reserved.