致力于创建骰子滚子

时间:2019-04-08 20:51:37

标签: python try-except

我正在创建一个D&D Dice-Roller供我使用,但是我遇到了问题。我希望能够掷出荒谬的骰子。因此,我需要知道数量部分应该在哪里结束,骰子的类型部分应该在哪里开始。 [我以以下方式键入纸卷:2d8(数量d类型)]。当我尝试使用.index()函数查找'd'字符时,我无法使其正常运行。 谢谢您的时间,即使您刚刚读过这篇文章。

import random

def rollDice(quant, type):
    Troll = 0
    for roll in range(quant):
        Troll += (random.randrange(type) + 1)
    print("You rolled: " + str(Troll))

def check(user):
    sep = user.lower().index('d') #I get the problem when I use this
    try:
        quant = int(user[0:sep])
        type = int(user[sep:])
        if type in (4, 6, 8, 10, 12, 20, 100):
            rollDice(quant, type)
        else:
            print("Not valid dice type!")
    except:
        print("Invalid Input!")

while True:
    print("To exit type [EXIT]")
    user = input("Enter what you want to roll[e.x: 2d8]: ")
    if user.upper() == "EXIT":
        exit()
    else:
        check(user)

1 个答案:

答案 0 :(得分:0)

解决此问题的简单方法:

async function fn() {
   await first();
   await second();
   await third();
}