如何执行“全部”和“一半”,而不是输入给定金额(经济)

huangapple go评论72阅读模式
英文:

How to do 'all' and 'half ' instead of entering a given amount (economy)

问题

如何使allhalf也能用于amount

您可以通过在代码中添加适当的条件来实现让allhalf也能用于amount。以下是一种可能的方式:

@client.command(aliases=['dep'])
async def deposit(ctx, amount=None):
    await open_account(ctx.author)
    user = ctx.author
    users = await get_bank_data()

    if amount is None:
        await ctx.send("Enter the amount you want to deposit")
        return

    bal = await update_bank(ctx.author)

    if amount.lower() == 'all':
        amount = bal[0]
    elif amount.lower() == 'half':
        amount = bal[0] / 2
    else:
        amount = int(amount)

    if amount > bal[0]:
        await ctx.send("You don't have that much money!")
        return
    if amount < 100:
        await ctx.send("You cannot deposit less than 100!")
        return

    await update_bank(ctx.author, -1 * amount)
    await update_bank(ctx.author, amount, "bank")

    await ctx.send(f"You have deposited {amount} to the bank!")

这段代码中,我们首先检查amount是否为allhalf,如果是,则将其分别设置为当前余额的全部或一半。否则,我们将amount解析为整数。然后,根据amount的值执行存款操作。

英文:

I have this code for a Discord bot command:

@client.command(aliases=[&#39;dep&#39;])
async def deposit(ctx, amount = None):
    await open_account(ctx.author)
    user = ctx.author
    users = await get_bank_data()

    if amount == None:
        await ctx.send(&quot;Enter the amount you want to deposit&quot;)
        return

    bal = await update_bank(ctx.author)

    amount = int(amount)
    if amount&gt;bal[0]:
        await ctx.send(&quot;You don&#39;t have that much money!&quot;)
        return
    if amount&lt;100:
        await ctx.send(&quot;You cannot deposit less than 100!&quot;)
        return
  
    await update_bank(ctx.author, -1*amount)
    await update_bank(ctx.author, amount, &quot;bank&quot;)

    await ctx.send(&quot;You have deposited {amount} to bank!&quot;)

How can I make it so that all and half can also be used for the amount?

答案1

得分: 1

以下是您要翻译的代码部分:

@client.command(aliases=['dep'])
async def deposit(ctx, amount = None):
    user = ctx.author
    await open_account(user)
    users = await get_bank_data()

    if amount is None:
        await ctx.send("请输入您要存款的金额")
        return

    bal = await update_bank(ctx.author)

    try:
        amount = int(amount)
    except ValueError:
        # 如果无法转换为整数,将引发错误
        # 检查是否等于一半或全部
        if amount == "half":
            amount = (bal[0] / 2)
        elif amount == "all":
            amount = bal[0]
        else:
            # 在这里,'amount' 既不是一半/全部,也无法转换为整数
            await ctx.send("请输入一个整数或者输入 'half' 或 'all'")
            return

    if amount > bal[0]:
        await ctx.send("您没有那么多钱!")
        return
    if amount < 100:
        await ctx.send("您不能存款少于100!")
        return
  
    await update_bank(ctx.author, -1*amount)
    await update_bank(ctx.author, amount, "bank")

    await ctx.send(f"您已存款 {amount} 到银行!")

请注意,代码中的注释也已翻译。

英文:

There are a couple of ways of doing this. Using your existing example - assuming amount is just a string then it's a relatively simple check.

@client.command(aliases=[&#39;dep&#39;])
async def deposit(ctx, amount = None):
    user = ctx.author
    await open_account(user)
    users = await get_bank_data()

    if amount is None:
        await ctx.send(&quot;Enter the amount you want to deposit&quot;)
        return

    bal = await update_bank(ctx.author)

    try:
        amount = int(amount)
    except ValueError:
        # will throw an error if amount is not castable to an int
        # check that it&#39;s equal to half or all
        if amount == &quot;half&quot;:
            amount = (bal[0] / 2)
        elif amount == &quot;all&quot;:
            amount = bal[0]
        else:
            # here, &#39;amount&#39; isn&#39;t half/all and can&#39;t be converted to an int
            await ctx.send(&quot;Please enter a whole number or half/all&quot;)
            return

    if amount &gt; bal[0]:
        await ctx.send(&quot;You don&#39;t have that much money!&quot;)
        return
    if amount &lt; 100:
        await ctx.send(&quot;You cannot deposit less than 100!&quot;)
        return
  
    await update_bank(ctx.author, -1*amount)
    await update_bank(ctx.author, amount, &quot;bank&quot;)

    await ctx.send(&quot;You have deposited {amount} to bank!&quot;)

You would just need to add to the command description that the user can enter 'half' or 'all' to deposit half/all.

huangapple
  • 本文由 发表于 2023年1月6日 13:33:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定