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

问题描述 投票:0回答:6

由于我是一个完全的编程新手,我需要您关于我需要完成在线课程的编码练习的建议。

这是说明:

mystery_int_1 = 3

mystery_int_2 = 4

mystery_int_3 = 5

(这些将在我提交代码后更改,所以这只是一个示例)

以上是三个值。运行 while 循环,直到所有三个值都小于或等于 0。每次更改三个变量的值时,都在同一行上打印出它们的新值,并用单个空格分隔。例如,如果它们的值分别为 3、4 和 5,您的代码将打印:

2 3 4

1 2 3

0 1 2

-1 0 1

-2 -1 0

我尝试这样写:

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) 

运行代码后我意识到它有问题,但我不知道如何修改它。尝试了很多选择,但都没有达到预期的效果...

提前谢谢大家!

python python-3.x debugging while-loop
6个回答
3
投票

您的循环当前会一直循环,直到任意数字小于或等于零。大声说出来,你的循环是“而神秘_int_1不小于或等于零并且神秘_int_2不小于或等于零并且......”,所以如果其中的任何小于或等于 0,循环停止。

你想要这个:

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

或者这个:

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

或者可能是这个,尽管我发现这个版本最令人困惑。 (也就是说,它最接近您已有的。)

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

0
投票

试试这个-

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))

阅读 python

and
AND `or


0
投票

你的条件

while not mystery_int_1 <= 0 and not mystery_int_2 <= 0 and not mystery_int_3 <= 0
是说循环而所有这些条件都为真(因为使用了和。)

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

当其中一个数字低于 0 时,一个条件变为 false,但循环的条件要求所有这些条件都为 true,因此循环会停止执行。您可能需要将

and
替换为
or
。因此,循环将继续,直到其中一个变量仍然大于 0。


0
投票

打开你的Python解释器并尝试以下操作,

假设我有一个有价值的数字

2

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

现在让我们检查数字是否>=0

>>> number>=0
True

是的!是真的。因为 number 大于 0。这是你应该做的。检查number是否>=0。但是相反你正在做,

>>> not number<=0
False

这意味着你正在做与你应该做的完全相反的事情,

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

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

因此,通过添加额外的

not
,您所做的事情与您想要的完全相反。所以在你的情况下这就足够了:

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

看看这个:

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

无限次打印

looop

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

但是这不会执行,因为有一个值是

0
。在Python中你不必检查是否
value>=0
。当一个值包含
0
时,它本身就被认为是错误的。所以你可以这样做:

while mystery_int_1 or mystery_int_2 or mystery_int_3:

这里即使有一个值为

not 0
>=0
循环也会运行。仅当其中一个值为
0
时才会退出。

完整代码如下:

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) 

输出:

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

0
投票

您可以尝试以下代码:

 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)

0
投票

我也在学习这门课程,这里的答案非常有帮助。我有一个问题:谁能解释一下为什么下面的代码与正确答案的效果不同?

while (mystery_int_1 or mystery_int_2 or 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)

换句话说,为什么 while 行与此不一样:

while mystery_int_1 > 0 or mystery_int_2 > 0 or mystery_int_3 > 0
© www.soinside.com 2019 - 2024. All rights reserved.