英文:
How to do 'all' and 'half ' instead of entering a given amount (economy)
问题
如何使all
和half
也能用于amount
?
您可以通过在代码中添加适当的条件来实现让all
和half
也能用于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
是否为all
或half
,如果是,则将其分别设置为当前余额的全部或一半。否则,我们将amount
解析为整数。然后,根据amount
的值执行存款操作。
英文:
I have this code for a Discord bot command:
@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 == None:
await ctx.send("Enter the amount you want to deposit")
return
bal = await update_bank(ctx.author)
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("You have deposited {amount} to bank!")
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=['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("Enter the amount you want to deposit")
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's equal to half or all
if amount == "half":
amount = (bal[0] / 2)
elif amount == "all":
amount = bal[0]
else:
# here, 'amount' isn't half/all and can't be converted to an int
await ctx.send("Please enter a whole number or half/all")
return
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("You have deposited {amount} to bank!")
You would just need to add to the command description that the user can enter 'half' or 'all' to deposit half/all.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论