如果一个用户达到一定的金额,我对此游戏有多个条件,那么如果另一个用户处于同等水平,他们会偷走更多的钱。
我在尝试弄清楚逻辑时遇到问题。我添加了一些评论,提出了问题,但我只是不知道是否有一种更简单的方法来处理这种事情。
启动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()
答案 0 :(得分:2)
请注意,如果两者均小于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
的情况,则可以使用独占ors来缩短它:
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
都是因为您不会到目前为止,如果那不是真的。
答案 1 :(得分:1)
[r in dict2[str_col] for r in dict1[str_col]]
是的,您确实会这样做,否则您的某些情况将包括其他情况,例如,如果# Do I need to put a check and money(message.author) < 500000 here?
和money(message.author) == 500001
,您可能希望money(user) == 1
发生,但它将触发{{ 1}}。
您可以将它们用于比较链,例如代替
dothing2
您可以使用更短的
dothing1
此外,定义两个临时变量if money(message.author) >= 20000 and money(message.author) < 500000 and money(user) < 20000:
dothing1
和if 20000 <= money(message.author) < 500000 and money(user) < 20000:
dothing1
可能会大大提高可读性。给定大量的ma = money(message.author)
和mu = money(user)
语句,您甚至可以定义if
,elif
等,但这可能是过大了。您还可以定义变量ma1 = ma < 20000
和ma2 = 20000 <= ma < 500000
,以防止输入错误(是四个零或五个?),并使其在将来更容易更改。
low = 20000