【问题标题】:Dice Rolling Simulator掷骰子模拟器
【发布时间】:2018-01-03 20:36:16
【问题描述】:

我正在用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()

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

【问题讨论】:

    标签: python-3.x simulator dice


    【解决方案1】:

    也许使用for-loop 并迭代用户想要掷骰子的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
    

    【讨论】:

    • 谢谢,我很感激。我是 python 新手,有时我会遇到麻烦。
    【解决方案2】:
    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!!")
    

    【讨论】:

      猜你喜欢
      • 2016-04-18
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多