【问题标题】:If, Elif, Greater than, Less than LogicIf、Elif、大于、小于逻辑
【发布时间】:2019-04-24 14:56:36
【问题描述】:

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

我在试图弄清楚逻辑时遇到了问题。我添加了 cmets 提问,但我只是不知道是否有更简单的方法来处理这类事情。

当我开始我的 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()

【问题讨论】:

  • 仔细观察,我认为我并没有在下面的答案中捕捉到所有内容。您究竟要处理哪些案例?

标签: python python-3.x discord.py


【解决方案1】:

请注意,如果 both 小于 20000,或者 both 大于 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

如果您不关心两者都有&lt; 20000 或两者都有&gt;= 20000 and &lt; 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) &lt; 500000 and ... 的检查,你就会知道money(message.author) &gt;= 20000money(user) &gt;= 20000,因为如果不是这样,你就不会走到这一步不是真的。

【讨论】:

  • 哇,我想我只是想多了!非常感谢您的快速回复,我会试试这个。我的问题是,如果money(message.author) &gt; 20000 and money(user) &lt; 20000 还会调用elif money(message.author) &lt; 20000 or money(user) &lt; 20000: 吗?
  • do_thing1`的情况不应该使用xor ^而不是or吗?
  • 我认为我没有在原始帖子中找到所有内容。我已经更新了它并添加了 cmets 来显示每个案例的捕获情况。我希望这会有所帮助。
  • 啊,我刚刚注意到您的回答中使用了or。这消除了对&gt;= and &lt;&lt; and &gt;= 的两个额外检查的需要。非常好的和彻底的解释。再次感谢!
【解决方案2】:
# Do I need to put a check and money(message.author) < 500000 here?

是的,您这样做了,否则您的某些案例将包括其他案例,例如,如果 money(message.author) == 500001money(user) == 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) 可能会极大地提高可读性。考虑到大量的ifelif 语句,您甚至可以定义ma1 = ma &lt; 20000ma2 = 20000 &lt;= ma &lt; 500000 等,但这可能是矫枉过正。您还可以定义变量 low = 20000high = 500000,防止拼写错误(是四个零还是五个?),并使其更容易在将来更改。

if low <= ma < high and mu < low: 
    do_thing1()
elif ma < low and low <= mu < high: 
    do_thing1()
elif low <= ma and low <= mu: 
    do_thing2()
elif high <= ma and mu < high: 
    do_thing2()
elif ma < high and high <= mu: 
    do_thing2()
elif high <= ma and high <= mu: 
    do_thing3()

【讨论】:

  • 哇,这个答案也很好。主要目标不是user + message.author钱的总和,而是检查他们在JSON文件中是否都有一定的金额,然后从user中取钱并将其添加到message.author中。代码很长,所以我试着简单地回答我的问题。如果他们都达到了一定数量的钱,那么被盗的金额就会被提高(当它被添加到 json 文件中时,通过一个声明的变量)但是如果一个人处于该特定级别,而另一个人不是,我想要它只取较低的金额。
猜你喜欢
  • 2019-01-08
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
相关资源
最近更新 更多