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

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

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

问题

如何使allhalf也能用于amount

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

  1. @client.command(aliases=['dep'])
  2. async def deposit(ctx, amount=None):
  3. await open_account(ctx.author)
  4. user = ctx.author
  5. users = await get_bank_data()
  6. if amount is None:
  7. await ctx.send("Enter the amount you want to deposit")
  8. return
  9. bal = await update_bank(ctx.author)
  10. if amount.lower() == 'all':
  11. amount = bal[0]
  12. elif amount.lower() == 'half':
  13. amount = bal[0] / 2
  14. else:
  15. amount = int(amount)
  16. if amount > bal[0]:
  17. await ctx.send("You don't have that much money!")
  18. return
  19. if amount < 100:
  20. await ctx.send("You cannot deposit less than 100!")
  21. return
  22. await update_bank(ctx.author, -1 * amount)
  23. await update_bank(ctx.author, amount, "bank")
  24. await ctx.send(f"You have deposited {amount} to the bank!")

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

英文:

I have this code for a Discord bot command:

  1. @client.command(aliases=[&#39;dep&#39;])
  2. async def deposit(ctx, amount = None):
  3. await open_account(ctx.author)
  4. user = ctx.author
  5. users = await get_bank_data()
  6. if amount == None:
  7. await ctx.send(&quot;Enter the amount you want to deposit&quot;)
  8. return
  9. bal = await update_bank(ctx.author)
  10. amount = int(amount)
  11. if amount&gt;bal[0]:
  12. await ctx.send(&quot;You don&#39;t have that much money!&quot;)
  13. return
  14. if amount&lt;100:
  15. await ctx.send(&quot;You cannot deposit less than 100!&quot;)
  16. return
  17. await update_bank(ctx.author, -1*amount)
  18. await update_bank(ctx.author, amount, &quot;bank&quot;)
  19. 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

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

  1. @client.command(aliases=['dep'])
  2. async def deposit(ctx, amount = None):
  3. user = ctx.author
  4. await open_account(user)
  5. users = await get_bank_data()
  6. if amount is None:
  7. await ctx.send("请输入您要存款的金额")
  8. return
  9. bal = await update_bank(ctx.author)
  10. try:
  11. amount = int(amount)
  12. except ValueError:
  13. # 如果无法转换为整数,将引发错误
  14. # 检查是否等于一半或全部
  15. if amount == "half":
  16. amount = (bal[0] / 2)
  17. elif amount == "all":
  18. amount = bal[0]
  19. else:
  20. # 在这里,'amount' 既不是一半/全部,也无法转换为整数
  21. await ctx.send("请输入一个整数或者输入 'half' 或 'all'")
  22. return
  23. if amount > bal[0]:
  24. await ctx.send("您没有那么多钱!")
  25. return
  26. if amount < 100:
  27. await ctx.send("您不能存款少于100!")
  28. return
  29. await update_bank(ctx.author, -1*amount)
  30. await update_bank(ctx.author, amount, "bank")
  31. 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.

  1. @client.command(aliases=[&#39;dep&#39;])
  2. async def deposit(ctx, amount = None):
  3. user = ctx.author
  4. await open_account(user)
  5. users = await get_bank_data()
  6. if amount is None:
  7. await ctx.send(&quot;Enter the amount you want to deposit&quot;)
  8. return
  9. bal = await update_bank(ctx.author)
  10. try:
  11. amount = int(amount)
  12. except ValueError:
  13. # will throw an error if amount is not castable to an int
  14. # check that it&#39;s equal to half or all
  15. if amount == &quot;half&quot;:
  16. amount = (bal[0] / 2)
  17. elif amount == &quot;all&quot;:
  18. amount = bal[0]
  19. else:
  20. # here, &#39;amount&#39; isn&#39;t half/all and can&#39;t be converted to an int
  21. await ctx.send(&quot;Please enter a whole number or half/all&quot;)
  22. return
  23. if amount &gt; bal[0]:
  24. await ctx.send(&quot;You don&#39;t have that much money!&quot;)
  25. return
  26. if amount &lt; 100:
  27. await ctx.send(&quot;You cannot deposit less than 100!&quot;)
  28. return
  29. await update_bank(ctx.author, -1*amount)
  30. await update_bank(ctx.author, amount, &quot;bank&quot;)
  31. 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:

确定