使用if和elif修复

huangapple go评论69阅读模式
英文:

Using If and Elif fix

问题

代码中的问题在于条件判断部分。要正确显示与百分比相关的消息,您需要调整条件和消息。以下是更正后的代码:

import random

print('你好,请告诉我你的愿望,我会告诉你它会实现的概率:')
wish = input()

percentage = random.randint(1, 100)
print('好的,' + wish + ',让我们看看......')

print(percentage, "% 的可能性会实现")

if percentage <= 75:
    boop = "机会很高,可能会实现"
elif percentage <= 50:
    boop = "嗯,可能会实现"
elif percentage <= 25:
    boop = "机会很低,可能不会实现"
elif percentage <= 1:
    boop = "非常低,祝你好运"
elif percentage == 0:
    boop = "太糟糕了,不会实现"
print(boop)

现在,代码将根据百分比的不同显示不同的消息,而不再出现错误的消息。

英文:

Code:

import random

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

percentage = random.randint(1, 100)
print(&#39;ok, &#39; + wish + &#39;, let us see.....&#39;)

print(percentage,&quot;% that you is&quot;)

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

I tried running the code, it work but the result is not what i was expecting it to be:

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

The percentage is 28% and it should say "low chance of happen, is will might no happen"
but instead, it says "wow is very high for happen". If someone could help me, please do.

答案1

得分: 0

你需要添加一个>= 50的条件,就像if (percentage <= 75)和其他地方一样。现在它对于任何小于75的值都返回true。或者,你可以将它反过来,使(percentage == 0)首先出现,然后if (percentage <= 75)放在最后。

英文:

You need to add a >= condition, like &gt;= 50, for the if (percentage &lt;= 75) and others. Right now it returns true for any value under 75. Alternatively, you could flip it so that (percentage == 0) is first and if (percentage &lt;= 75) is last.

答案2

得分: 0

Replace &lt; sign with &gt; sign.

Reason: 28 is less than 75 so percentage &lt;= 75 is true and you get wow is very high for happen. It should be percentage &gt;= 75 and so when you see the condition percentage &gt;= 25 in elif (percentage &gt;= 25) :, you'll get correct output low chance of happen, is will might no happen.

英文:

Replace &lt; sign with &gt; sign.

Reason: 28 is less than 75 so percentage &lt;= 75 is true and you get wow is very high for happen. It should be percentage &gt;= 75 and so when you see the condition percentage &gt;= 25 in elif (percentage &gt;= 25) :, you'll get correct output low chance of happen, is will might no happen


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

答案3

得分: 0

以下是您要翻译的代码部分:

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)
英文:

There were few mistakes like:

  1. You didn't have a else case so if the percentage is greater or equal to 75, there will be an error.
  2. You are starting your comparison from a higher point that is percentage<75 which will be always true.
import random
percentage = random.randint(1, 100)

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

</details>



# 答案4
**得分**: 0

将`if`和`elif`语句中的`&lt;=`替换为`&gt;=`。这样,在您的语句按顺序排列时,它将逐个比较,始终落入正确的类别。

<details>
<summary>英文:</summary>

Replace your `&lt;=` in the `if`, `elif` statements to `&gt;=`. This way, in the order your statements are in, it will compare going down the list and it will always fall into the correct category.

</details>



huangapple
  • 本文由 发表于 2020年1月4日 12:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/59587865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定