骰子滚动模拟器

时间:2017-07-27 22:49:01

标签: python-3.x simulator dice

我在python中制作一个Dice滚动模拟器来播放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()

我最初的注意力只是为了让它成为一个数字变量,但是我哥哥提醒我,有些武器有多个卷而不是只滚动不止一次,我想要输入多少次掷骰子。我的代码现在的问题是它将随机化数字,然后乘以2。我想要它做的是将不同整数的数量乘以并将它们加在一起。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

• Let S be the length of the longest string.
• Let a be the length of the array.
Now we can work through this in parts:
• Sorting each string isO(s lo g s),
• We have to do this for every string (and there are a strings), so that's 0( a* s lo g s).
• Now we have to sort all the strings. There a re a strings, so you'll may be inclined to say that this takes 0( a
l o g a) time. This is what most candidates would say. You should also take into account that you need
to compare the strings. Each string comparison takes 0(s ) time.There are 0( a lo g a) comparisons,
therefore this will takeO(a* s lo g a)time .
If you add up these two parts, you get 0(a*s ( lo g a + lo g s)) .