Discord 机器人掷骰子错误。为什么我的代码达不到预期的结果?
Discord bot dice roll error. Why does my code not reach its expected result?
我的 Python Discord Bot 目前有点问题。我尝试在我的机器人中编写的代码部分是一个简单的掷骰子命令,但是无论我尝试什么,我似乎都无法弄清楚如何修复它。
我正在尝试编写的命令是“!roll d(骰子的面数)(骰子的数量),然后应该 return 用面数指定的骰子掷骰数。例如,输入“!roll d20 4”的人应该 return 类似 "Your dice rolls were: 13, 6, 18, 3" 的内容。这是我目前拥有的代码:
@client.command()
async def roll(ctx, sides, amount):
try:
sides = sides.split("d")
rolls = []
for number in range(amount):
result = random.randint(sides[1])
rolls.append(result)
rolls = ", ".join(rolls)
await ctx.send("Your dice rolls were: " + rolls)
except:
await ctx.send("Incorrect format for sides of dice (try something like \"!roll d6 1\").")
当我 运行 程序时,我没有收到任何错误,即使试图将主要部分移到 "try" 部分之外,我也没有收到任何错误,但仍然没有收到预期的结果,像这样:
try:
sides = sides.split("d")
check = True
except:
await ctx.send("Incorrect format for sides of dice (try something like \"!roll d6 1\").")
if check == True:
blah blah rest of code
我在您的代码中发现了 4 个错误:
- 你说你没有收到任何错误,那是因为你使用的是 bare
except
。像 这样简单的东西
except Exception as e:
print(e)
会给你错误信息。如果您想要更多,您还可以打印回溯以查明错误代码。
random.randint
有两个参数 start
和 end
并且都是 int
.
现在你只超过了一个,甚至都不是 int
。
sides[1]
会给你一个字符串,即使这个字符串包含一个数字,但类型仍然是一个字符串,因为 .split
returns 一个字符串列表。因此,例如您调用了 !roll d3 5
,那么 sides
将是一个列表 ["d", "3"]
,其中 sides[1]
将是一个字符串 "3"
您的 rolls
将是一个整数列表,因为 random.randint
returns 一个 int
而您正在使用 rolls .append(result)
因此 rolls
将是整数列表。
所以你不能使用 ", ".join(rolls)
因为你会将整数连接到字符串 ", "
相反,您需要调用 ", ".join(str(number) for number in rolls)
或者您可以立即将每个追加调用转换为字符串。
amount
将作为字符串传递,因此您不能使用 range(amount)
它需要是 range(int(amount))
完整代码:
async def roll(ctx, sides, amount):
try:
sides = int(sides.split("d")[1])
rolls_list = []
for number in range(int(amount)):
# 1 is the minimum number the dice can have
rolls_list.append(random.randint(1, sides))
rolls = ", ".join(str(number) for number in rolls_list)
await ctx.send("Your dice rolls were: " + rolls)
except Exception as e:
# You should catch different exceptions for each problem and then handle them
# Exception is too broad
print(e)
await ctx.send("Incorrect format for sides of dice (try something like \"!roll d6 1\").")
您还应该检查一些输入错误,例如整数是否为负数 amount
我的 Python Discord Bot 目前有点问题。我尝试在我的机器人中编写的代码部分是一个简单的掷骰子命令,但是无论我尝试什么,我似乎都无法弄清楚如何修复它。
我正在尝试编写的命令是“!roll d(骰子的面数)(骰子的数量),然后应该 return 用面数指定的骰子掷骰数。例如,输入“!roll d20 4”的人应该 return 类似 "Your dice rolls were: 13, 6, 18, 3" 的内容。这是我目前拥有的代码:
@client.command()
async def roll(ctx, sides, amount):
try:
sides = sides.split("d")
rolls = []
for number in range(amount):
result = random.randint(sides[1])
rolls.append(result)
rolls = ", ".join(rolls)
await ctx.send("Your dice rolls were: " + rolls)
except:
await ctx.send("Incorrect format for sides of dice (try something like \"!roll d6 1\").")
当我 运行 程序时,我没有收到任何错误,即使试图将主要部分移到 "try" 部分之外,我也没有收到任何错误,但仍然没有收到预期的结果,像这样:
try:
sides = sides.split("d")
check = True
except:
await ctx.send("Incorrect format for sides of dice (try something like \"!roll d6 1\").")
if check == True:
blah blah rest of code
我在您的代码中发现了 4 个错误:
- 你说你没有收到任何错误,那是因为你使用的是 bare
except
。像 这样简单的东西
except Exception as e:
print(e)
会给你错误信息。如果您想要更多,您还可以打印回溯以查明错误代码。
random.randint
有两个参数start
和end
并且都是int
.现在你只超过了一个,甚至都不是
int
。sides[1]
会给你一个字符串,即使这个字符串包含一个数字,但类型仍然是一个字符串,因为.split
returns 一个字符串列表。因此,例如您调用了!roll d3 5
,那么sides
将是一个列表["d", "3"]
,其中sides[1]
将是一个字符串"3"
您的
rolls
将是一个整数列表,因为random.randint
returns 一个int
而您正在使用rolls .append(result)
因此rolls
将是整数列表。所以你不能使用
", ".join(rolls)
因为你会将整数连接到字符串", "
相反,您需要调用
", ".join(str(number) for number in rolls)
或者您可以立即将每个追加调用转换为字符串。amount
将作为字符串传递,因此您不能使用range(amount)
它需要是range(int(amount))
完整代码:
async def roll(ctx, sides, amount):
try:
sides = int(sides.split("d")[1])
rolls_list = []
for number in range(int(amount)):
# 1 is the minimum number the dice can have
rolls_list.append(random.randint(1, sides))
rolls = ", ".join(str(number) for number in rolls_list)
await ctx.send("Your dice rolls were: " + rolls)
except Exception as e:
# You should catch different exceptions for each problem and then handle them
# Exception is too broad
print(e)
await ctx.send("Incorrect format for sides of dice (try something like \"!roll d6 1\").")
您还应该检查一些输入错误,例如整数是否为负数 amount