[英]Nesting If and Else statements for Discord.Py
我已經為此命令苦苦掙扎了一段時間。 我可以拿一張支票上班,但不能拿另一張。 我想確保用戶不會提及自己,並且如果他們從get_dollars提取的錢少於0美元,那么他們就不能“搶劫”另一個用戶。 我嘗試過"try, except, else, finally"
並使其半成品,但它會吐出兩條消息,而不是一條消息。 然后,我嘗試下面的代碼,以考慮是否嵌套if
和else
語句,它將更好地工作。 我得到一個if message.author == user.mention: UnboundLocalError: local variable 'user' referenced before assignment
錯誤以這種方式嘗試if message.author == user.mention: UnboundLocalError: local variable 'user' referenced before assignment
。
if message.content.startswith('!rob'):
if message.author == user.mention:
await client.send_message(message.channel, "{} you cant rob yourself! 👊".format(message.author.mention))
else:
if get_dollars(message.author) < 0:
await client.send_message(message.channel, "{} you can't even afford a gun.".format(message.author.mention))
else:
for user in message.mentions:
if (get_dollars(user)) < 1:
await client.send_message(message.channel, "{} is too broke to rob 🤣".format(user.mention))
elif steal <= 3:
print("{} catches {} slippin and sticks them up for ${} 😨🔫".format(message.author.mention, user.mention, stealdollars))
await client.send_message(message.channel, "{} catches {} slippin and sticks them up for ${} 😨🔫".format(message.author.mention, user.mention, stealdollars))
remove_dollars(user, stealdollars)
add_dollars(message.author, stealdollars)
elif steal == 4:
print("{} gets arrested trying to rob {} and has to post $250 bail 👮🚨".format(message.author.mention, user.mention))
await client.send_message(message.channel, "{} gets arrested trying to rob {} and has to post $250 bail 👮🚨".format(message.author.mention, user.mention))
remove_dollars(message.author, 250)
elif steal >= 5:
print("{} kicks {}'s door down but doesn't find any cash 🤬".format(message.author.mention, user.mention))
await client.send_message(message.channel, "{} kicks {}'s door down but doesn't find any cash 🤬".format(message.author.mention, user.mention))
就像Patrick所說的那樣,沒有用戶的定義。 最終將user = message.mentions [0]放進去,命令運行正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.