简体   繁体   English

if 和 else 的问题(python,discord.py)

[英]Problem with if and else (python, discord.py)

recently I started making discord bots using python.最近我开始使用 python 制作 discord 机器人。 I made some commands like checking how much user1 loves user2 .我做了一些命令,比如检查user1user2的喜爱程度。 user1/2 cannot be the bot itself or the same person. user1/2不能是机器人本身或同一个人。 It worked fine most of the time, but when I added other command like this everything stops working.它大部分时间都运行良好,但是当我添加其他这样的命令时,一切都停止了。 Here's my code:这是我的代码:

@bot.command()
@commands.check(user_is_me)
async def milosc(ctx, user1: discord.Member, user2: discord.Member):
    milosc_procent = random.randint(1, 101)
    if user1 == user2:
        await ctx.send("**Nie możesz sprawdzic jak bardzo ktoś kocha samego siebie**")
    if user1.name == 'Pieseł bot':
        await ctx.send("**Nie możesz sprawdzić jak bardzo bot kogoś kocha. To nie ma sensu !**")
    if user2.name == 'Pieseł bot':
        embed=discord.Embed(title="Jak bardzo kochasz Pieseł bot-a", description=f"Użytkownik {user1.name} kocha użytkownika {user2.name} w 100%. Bo kto takiego słodkiego piesełka by nie kochał <:pepeLove:831157130153164831>", color=0xff0000, timestamp=datetime.datetime.utcnow())
        embed.set_author(name="Pieseł bot", icon_url="https://cdn.discordapp.com/avatars/451734929999659008/ed54c994c64b2e0df385822e42c48993.png?size=4096")
        embed.set_footer(text=f"Sprawdzono na prośbę {ctx.author.name}")
        await ctx.send(embed=embed)
    else:
        embed=discord.Embed(title="Jak bardzo się kochają", description=f"Użytkownik {user1.name} kocha użytkownika {user2.name} w {milosc_procent}%", color=0xff0000, timestamp=datetime.datetime.utcnow())
        embed.set_author(name="Pieseł bot", icon_url="https://cdn.discordapp.com/avatars/451734929999659008/ed54c994c64b2e0df385822e42c48993.png?size=4096")
        embed.set_footer(text=f"Sprawdzono na prośbę {ctx.author.name}")
        await ctx.send(embed=embed)
        if milosc_procent >= 100:
            await ctx.send(file=discord.File('milosc.png'))
        
@bot.command()
@commands.check(user_is_me)
async def para(ctx, user1: discord.Member, user2: discord.Member):
    how_para = random.randint(1, 101)
    if user1 == user2:
        await ctx.send("**Nie możesz sprawdzic czy ktoś jest parą z samym sobą :exploding_head:**")
    if user1.name == 'Pieseł bot':
        await ctx.send("**Nie możesz sprawdzić jak bardzo bot pasuje do bycia z kimś parą. To nie ma sensu !**")
    if user2.name == 'Pieseł bot':
        await ctx.send("**Nie możesz sprawdzić jak bardzo bot pasuje do bycia z kimś parą. To nie ma sensu !**")
    else:
        embed=discord.Embed(title="Jak bardzo pasują jako para", description=f"Użytkownik {user1.name} z użytkownikiem {user2.name} byli by zarąbistą parą w {how_para}%", color=0xff0000, timestamp=datetime.datetime.utcnow())
        embed.set_author(name="Pieseł bot", icon_url="https://cdn.discordapp.com/avatars/451734929999659008/ed54c994c64b2e0df385822e42c48993.png?size=4096")
        embed.set_footer(text=f"Sprawdzono na prośbę {ctx.author.name}")
        await ctx.send(embed=embed)
        if how_para >= 100:
            await ctx.send(file=discord.File('para.gif'))

It should look like this:它应该如下所示:它应该是什么样子

But it's look like this:但它看起来像这样:它看起来如何

Bot is for Polish users, that's why all messages are in polish. Bot 是为波兰语用户准备的,这就是为什么所有消息都是波兰语的。

If you want to make 1 loop with multiple 'if' statements you should use 'elif'.如果你想用多个'if'语句创建1个循环,你应该使用'elif'。 'Elif' is short for else if. 'Elif' 是 else if 的缩写。 If the condition for the if is False , the code will check the condition elif .如果if的条件是False ,代码将检查条件elif If all the conditions if and elif are false the code will execute the else block.如果ifelif的所有条件都为 false,则代码将执行else块。 See the example below.请参见下面的示例。

user1 = 'mike'
user2 = 'paul'

if user1 == user2:
    print('User 1 is equal to user 2')
elif user1 == 'paul':
    print('User 1 is Paul')
else:
    print('No users recognized')

Your code will now execute the else block in the last if - else statement each time the last if condition is not satisfied.现在,您的代码将在每次最后一个if条件不满足时执行最后一个if - else语句中的else块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM