gpt4 book ai didi

python - If、Elif、大于、小于逻辑

转载 作者:太空宇宙 更新时间:2023-11-04 00:00:42 25 4
gpt4 key购买 nike

我对这个游戏有多个条件,如果一个用户达到一定数量的钱,那么如果其他用户达到他们的水平,他们偷的钱就越多。

我在尝试找出其中的逻辑时遇到了问题。我添加了评论来提问,但我只是不知道是否有更简单的方法来处理此类问题。

当我开始 if 语句时,我应该从较小的值开始还是应该从较大的值开始?

# do I need to put a check and money(message.author) < 500000 here?
if money(message.author) >= 20000 and money(user) < 20000:
do_thing1()
# do I need to put a check and money(user) < 500000 here?
elif money(message.author) < 20000 and money(user) >= 20000:
do_thing1()
# do I need to put a check money(message.author) and money(user) < 500000 here?
elif money(message.author) >= 20000 and money(user) >= 20000:
do_thing2()
# do I need to put a check and money(user) >= 20000 here?
elif money(message.author) >= 500000 and money(user) < 500000:
do_thing2()
# do I need to put a check and money(message.author) >= 20000 here?
elif money(message.author) < 500000 and money(user) >= 500000:
do_thing2()
elif money(message.author) >= 500000 and money (user) >= 500000:
do_thing3()

最佳答案

请注意,如果两者 都小于 20000,或者如果两者 都大于 20000 但小于 500000,您不会显示会发生什么。我将显示那些下面进行处理。

if money(message.author) < 20000 and money(user) < 20000:
do_thing0() # both have < 20000. You didn't show this case.
elif money(message.author) < 20000 or money(user) < 20000:
do_thing1() # only one of them has < 20000
elif money(message.author) < 500000 and money(user) < 500000:
do_thingX() # both have >= 20000 but < 500000. You didn't show this case.
elif money(message.author) < 500000 or money(user) < 500000:
do_thing2() # both have >= 20000, only one of them has < 5000000
else:
do_thing3() # both have >= 500000

如果您不关心两者都有 < 20000 的情况或者两者都有>= 20000 and < 500000 , 然后你可以用异或来缩短它:

elif money(message.author) < 20000 ^ money(user) < 20000:
do_thing1() # only one of them has < 20000
elif money(message.author) < 500000 ^ money(user) < 500000:
do_thing2() # both have >= 20000, only one of them has < 5000000
elif money(message.author) >= 500000 and money(user) >= 50000
do_thing3() # both have >= 500000

请注意,在最后一个案例中我们需要明确,否则我们将捕获前两个案例未捕获的任何,这里我假设您不想例如,捕获两者都小于 20000 的情况。

您的代码按顺序执行,因此如果您通过了其中一项检查,就会告诉您一些关于您所处范围的信息,您可以相应地调整您的逻辑。

例如,一旦您到达了 elif money(message.author) < 500000 and ...检查一下,你知道这两个 money(message.author) >= 20000 money(user) >= 20000因为如果那不是真的,你就不会走到这一步。

关于python - If、Elif、大于、小于逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55833045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com