简体   繁体   English

Python while循环(编码练习,三个变量)

[英]Python while loop (coding exercise, three variables)

As I'm a total programming newbie, I need your advice concerning the coding exercise which I need to fulfil for the online course. 因为我是一名编程新手,所以我需要您提供有关在线课程需要完成的编码练习的建议。

Here is the instruction: 这是说明:

mystery_int_1 = 3

mystery_int_2 = 4

mystery_int_3 = 5
(these will be changed after I submit the code, so it's just an example) (在我提交代码后,这些内容将被更改,因此这只是一个示例)

Above are three values. 以上是三个值。 Run a while loop until all three values are less than or equal to 0. Every time you change the value of the three variables, print out their new values all on the same line, separated by single spaces. 运行while循环,直到所有三个值都小于或等于0。每次更改三个变量的值时,将它们的新值全部打印在同一行上,并用单个空格隔开。 For example, if their values were 3, 4, and 5 respectively, your code would print: 例如,如果它们的值分别为3、4和5,则您的代码将输出:

2 3 4 2 3 4

1 2 3 1 2 3

0 1 2 0 1 2

-1 0 1 -1 0 1

-2 -1 0 -2 -1 0

I tried writing it this way: 我试过这样写:

while not mystery_int_1 <= 0 and not mystery_int_2 <= 0 and not mystery_int_3 <= 0: 
    mystery_int_1 -= 1
    mystery_int_2 -= 1
    mystery_int_3 -= 1
    print(mystery_int_1, mystery_int_2, mystery_int_3) 

After running the code I realized that there is something wrong with it, but I cannot figure out how to modify it. 运行代码后,我意识到它有问题,但是我无法弄清楚如何对其进行修改。 Tried many options, and neither of them worked as it was supposed to... 尝试了许多选择,但它们都不如预期的那样工作。

Thank you all in advance! 谢谢大家!

Your loop currently loops until any of the numbers is less than or equal to zero. 您的循环当前循环直到任何数字小于或等于零为止。 Said out loud, your loop is "while mystery_int_1 isn't less than or equal to zero and mystery_int_2 isn't less than or equal to zero and ...", so if any of them is less than or equal to zero, the loop stops. 大声说,你的循环是“而mystery_int_1不小于或等于零, 并且 mystery_int_2不小于或等于零 ......”,所以,如果他们 任何一个小于或等于零时,循环停止。

You want this: 你要这个:

while not (mystery_int_1 <= 0 and mystery_int_2 <= 0 and mystery_int_3 <= 0):

Or this: 或这个:

while mystery_int_1 > 0 or mystery_int_2 > 0 or mystery_int_3 > 0:

Or possibly this, though I find this version the most confusing. 也许是这样,尽管我觉得这个版本最令人困惑。 (That said, it's closest to what you already have.) (也就是说,它与您已经拥有的最接近。)

while not mystery_int_1 <= 0 or not mystery_int_2 <= 0 or not mystery_int_3 <= 0:

try this - 尝试这个 -

while not mystery_int_1 <= 0 or not mystery_int_2 <= 0 or not mystery_int_3 <= 0: 
   mystery_int_1 -= 1
   mystery_int_2 -= 1
   mystery_int_3 -= 1
   print("{} {} {}".format(mystery_int_1, mystery_int_2, mystery_int_3))

Read python and AND `or 阅读python and AND或

Your condition while not mystery_int_1 <= 0 and not mystery_int_2 <= 0 and not mystery_int_3 <= 0 is saying loop while all these conditions are true (because of the and used.) : 当您的条件while not mystery_int_1 <= 0 and not mystery_int_2 <= 0 and not mystery_int_3 <= 0您的条件就是说循环,而所有这些条件都是真实的(由于和。)

  • mystery_int_1>0 mystery_int_1> 0
  • mystery_int_2>0 mystery_int_2> 0
  • mystery_int_3>0 mystery_int_3> 0

When one of the number goes below 0, one condition becomes false but the condition for your loop requires all those to be true thus it stops executing. 当数字之一低于0时,一个条件为假,但是循环的条件要求所有条件都为真,因此它将停止执行。 You might want to replace and with or . 您可能需要更换andor So, that the loop continues until one of the variable is still greater than 0. 因此,循环继续进行,直到变量之一仍大于0为止。

Open your python interpreter and try the following, 打开您的python解释器,然后尝试以下操作:

Let's assume I have a number which has value 2 假设我有一个值为2

>>> number = 2
>>> number-=1  #2-1=1
>>> number
1

Now let's check if number>=0 现在让我们检查number> = 0

>>> number>=0
True

Yes! 是! It is True. 是真的。 Because number is greater than 0. This what you should be doing . 因为number大于0。 这是您应该做的 Checking whether number>=0. 检查数字是否> = 0。 But Instead you are doing, 但是相反,你在做,

>>> not number<=0
False

Meaning you are doing the exact opposite of what you should be doing, 意味着您所做的与您应该做的完全相反,

>>> number-=2
>>> number 
-1    #now number has become -1

>>> number>=0
False
>>> not number<=0
True

So by adding that extra not you are just doing the exact opposite of what you want. 因此,通过增加额外的not你只是在做你想要什么的完全相反。 So in your case just this is enough: 因此,在您的情况下,这就足够了:

while mystery_int_1 > 0 or mystery_int_2 > 0 or mystery_int_3 > 0:

Take a look at this: 看看这个:

value=1
value1=2
value2 =3
while value or value2 or value1:
    print("looop")

Prints infinte times looop . 打印无限次looop

value=0
value1=2
value2 =3
while value and value2 and value1:
    print("looop")

However this doesn't execute because one value is 0 . 但是,这不会执行,因为一个值为0 In python you don't have to check if value>=0 . 在python中,您不必检查value>=0 The moment a value has 0 in it that itself is considered a false. 值中包含0的那一刻,其本身就被视为错误。 So you can just do this: 因此,您可以执行以下操作:

while mystery_int_1 or mystery_int_2 or mystery_int_3:

Here even if one value is not 0 ie >=0 loop will run. 即使一个值not 0>=0循环也将在此处运行。 It will exit only when one of the values is 0 . 仅当值之一为0时,它将退出。

Here is the complete code: 这是完整的代码:

mystery_int_1 = 3
mystery_int_2 = 4
mystery_int_3 = 5 
while mystery_int_1 or mystery_int_2 or mystery_int_3: 
    mystery_int_1 -= 1
    mystery_int_2 -= 1
    mystery_int_3 -= 1
    print(mystery_int_1, mystery_int_2, mystery_int_3) 

output: 输出:

2 3 4
1 2 3
0 1 2
-1 0 1
-2 -1 0

You can try this code: 您可以尝试以下代码:

 while not (mystery_int_1<=0 and mystery_int_2 <=0 and mystery_int_3<=0):

    mystery_int_1 -= 1
    mystery_int_2 -= 1
    mystery_int_3 -= 1

    print(mystery_int_1, mystery_int_2, mystery_int_3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM