使用 If 和 Elif 修复

Using If and Elif fix

代码:

import random

print('hello, pls give me ur wish and me will tell how many % it will happen.')
wish = input()

percentage = random.randint(1, 100)
print('ok, ' + wish + ', let us see.....')

print(percentage,"% that you is")

if (percentage <= 75) :
  boop = "wow is very high for happen"
elif (percentage <= 50) :
  boop = "hmmm, is will happen maybe"
elif (percentage <= 25) :
  boop = "low chance of happen, is will might no happen"
elif (percentage <= 1) :
  boop = "very low, wish u lucc"
elif (percentage == 0) :
  boop = "too bad so sad, is no happen"
print(boop)

我尝试了 运行 代码,它可以工作,但结果不是我所期望的:

I want to fly.
ok, I want to fly., let us see.....
28 % that you is
wow is very high for happen
>>> 

百分比是 28%,应该是 "low chance of happen, is will might no happen" 但相反,它显示 "wow is very high for happen"。如果有人可以帮助我,请帮助我。

您需要为 if (percentage <= 75) 和其他人添加一个 >= 条件,例如 >= 50。现在,对于任何小于 75 的值,它 returns 为真。或者,您可以翻转它,使 (percentage == 0) 位于第一个,if (percentage <= 75) 位于最后一个。

< 符号替换为 > 符号。

原因: 28 小于 75 所以 percentage <= 75 为真,得到 wow is very high for happen。它应该是 percentage >= 75 所以当你在 elif (percentage >= 25) : 中看到条件 percentage >= 25 时,你将得到正确的输出 low chance of happen, is will might no happen


percentage = 28
if (percentage >= 75) :
  boop = "wow is very high for happen"
elif (percentage >= 50) :
  boop = "hmmm, is will happen maybe"
elif (percentage >= 25) :
  boop = "low chance of happen, is will might no happen"
elif (percentage >= 1) :
  boop = "very low, wish u lucc"
elif (percentage == 0) :
  boop = "too bad so sad, is no happen"

很少有错误,例如:

  1. 您没有 else 情况,因此如果百分比大于或等于 75,则会出现错误。
  2. 您是从较高的点开始比较的,即百分比 <75,这将始终为真。
import random
percentage = random.randint(1, 100)

print(percentage,"% that you is")
if (percentage == 0) :
    boop = "too bad so sad, is no happen"
elif (percentage <= 1) :
    boop = "very low, wish u lucc"
elif (percentage <= 25) :
    boop = "low chance of happen, is will might no happen"
elif (percentage <= 50) :
    boop = "hmmm, is will happen maybe"
elif (percentage <= 75) :
    boop = "wow is very high for happen"
else:
    boop="above 75"
print(boop)```

ifelif 语句中的 <= 替换为 >=。这样,按照你的陈述的顺序,它会比较列表中的向下,它总是属于正确的类别。