【问题标题】:Python while loop (coding exercise, three variables)Python while 循环(编码练习,三个变量)
【发布时间】:2017-12-01 06:36:42
【问题描述】:

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

这里是指令:

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


【解决方案1】:

你可以试试这个代码:

 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)

【讨论】:

    【解决方案2】:

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

    假设我有一个值为2的数字

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

    现在让我们检查一下 number>=0

    >>> number>=0
    True
    

    是的!是真的。因为数字大于 0。这是你应该做的。检查数字是否>=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&gt;=0。当一个值中有 0 时,它本身被认为是错误的。所以你可以这样做:

    while mystery_int_1 or mystery_int_2 or mystery_int_3:
    

    即使一个值为not 0,即&gt;=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
    

    【讨论】:

    • 这基本上是对的,但您的最终代码使用and 而不是or
    • 我通常也更喜欢if foo &gt; 0 而不是if foo。更难犯错误,例如在该变量中意外包含字符串。 “显式胜于隐式!”
    • 是的...until all values are 0我错过了谢谢伙计..我会更新。顺便说一句 +1,你的解决方案很棒
    • 其实你应该使用mystery_int_1 &gt; 0,而不仅仅是mystery_int_1。负数在 Python 中被认为是真实的。你真的运行过你的代码吗?我认为您的循环将永远运行。
    【解决方案3】:

    您的条件while not mystery_int_1 &lt;= 0 and not mystery_int_2 &lt;= 0 and not mystery_int_3 &lt;= 0 表示循环,而所有这些条件都为真(因为 and used.)

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

    当其中一个数字低于 0 时,一个条件变为假,但循环的条件要求所有条件都为真,因此它停止执行。您可能希望将and 替换为or。因此,循环继续,直到其中一个变量仍然大于 0。

    【讨论】:

      【解决方案4】:

      试试这个 -

      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

      【讨论】:

        【解决方案5】:

        您的循环当前循环,直到 任何 个数字小于或等于零。大声说出来,您的循环是“而神秘整数1 不小于或等于零神秘整数_2 不小于或等于零 ...”,因此,如果 any 其中 小于或等于 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:
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-20
          • 2017-01-11
          • 2017-04-03
          • 1970-01-01
          • 1970-01-01
          • 2023-02-05
          • 1970-01-01
          相关资源
          最近更新 更多