Python “全部付款”命令的if all参数值错误

Python “全部付款”命令的if all参数值错误,python,python-3.x,discord,discord.py,discord.py-rewrite,Python,Python 3.x,Discord,Discord.py,Discord.py Rewrite,我的代码可以工作,但当我包含pay'all'命令时,它似乎给了我一个错误。 我希望机器人基本上在我告诉它的时候就停止。pay@example#0001 all“它选择所有的“bal” 我的代码: @client.command(别名=['send']) 异步def支付(ctx,成员:discord.member,amount=None): 等待开户(ctx.author) 等待开户(会员) 如果金额==无: 等待ctx.send('请输入金额') 返回 bal=等待更新银行(ctx.author

我的代码可以工作,但当我包含pay'all'命令时,它似乎给了我一个错误。 我希望机器人基本上在我告诉它的时候就停止。pay@example#0001 all“它选择所有的“bal”

我的代码:

@client.command(别名=['send'])
异步def支付(ctx,成员:discord.member,amount=None):
等待开户(ctx.author)
等待开户(会员)
如果金额==无:
等待ctx.send('请输入金额')
返回
bal=等待更新银行(ctx.author)
金额=整数(金额)
如果金额=‘全部’:#这
金额=余额[0]#不起作用
如果金额>余额[1]:
等待ctx.send(“你没有那么多钱!”)
返回
如果金额更改此值:

    amount = int(amount)
    if amount == 'all':     #This
        amount = bal[0]     #Wont work
为此:

    if amount == 'all':     #This
        amount = bal[0]     #Wont work
    else:
        try:
            amount = int(amount)
        except ValueError:
            await ctx.send(f'Invalid amount({amount}) Must be "all" or an integer')
            return
            
这里的想法是,您希望检查是否发送了“all”,如果没有,请尝试将其转换为整数。如果无法强制转换为int(),则会出现ValueError,如您所述。捕获ValueError,然后向用户回复消息。

更改此选项:

    amount = int(amount)
    if amount == 'all':     #This
        amount = bal[0]     #Wont work
为此:

    if amount == 'all':     #This
        amount = bal[0]     #Wont work
    else:
        try:
            amount = int(amount)
        except ValueError:
            await ctx.send(f'Invalid amount({amount}) Must be "all" or an integer')
            return
            

这里的想法是,您希望检查是否发送了“all”,如果没有,请尝试将其转换为整数。如果无法强制转换为int(),则会出现ValueError,如您所述。捕获ValueError,然后向用户回复消息。

更改此选项:


如果金额==无:
等待ctx.send('请输入金额')
返回
bal=等待更新银行(ctx.author)
金额=整数(金额)
如果金额=‘全部’:#这
金额=余额[0]#不起作用
至:

bal=wait update\u bank(ctx.author)
如果金额==“全部”:
金额=余额[0]
其他:
尝试:
金额=整数(金额)
除值错误外:
wait ctx.send(f'Invalid amount({amount})必须是“all”或整数')
返回
amount=int(amount)#这必须在定义“amount==‘all’之后
#代码的其余部分

更改此项:


如果金额==无:
等待ctx.send('请输入金额')
返回
bal=等待更新银行(ctx.author)
金额=整数(金额)
如果金额=‘全部’:#这
金额=余额[0]#不起作用
至:

bal=wait update\u bank(ctx.author)
如果金额==“全部”:
金额=余额[0]
其他:
尝试:
金额=整数(金额)
除值错误外:
wait ctx.send(f'Invalid amount({amount})必须是“all”或整数')
返回
amount=int(amount)#这必须在定义“amount==‘all’之后
#代码的其余部分

尝试转换为int后无法检查字符串,因为如果该字符串不是有效数字,则已经太晚了。在任何情况下,您都需要防止出现异常,以防用户键入其他类型的垃圾。您无法在尝试转换为int后检查字符串,因为如果该字符串不是有效数字,则已经太晚了。在任何情况下,您都需要防止出现异常,以防用户键入其他类型的垃圾。感谢用户:3339758提供额外代码..感谢用户:3339758提供额外代码。。