1

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. For example, if their values were 3, 4, and 5 respectively, your code would print:

2 3 4

1 2 3

0 1 2

-1 0 1

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

Exa
  • 4,020
  • 7
  • 43
  • 60

5 Answers5

3

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.

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:
user94559
  • 59,196
  • 6
  • 103
  • 103
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

tom
  • 3,720
  • 5
  • 26
  • 48
0

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

  • mystery_int_1>0
  • mystery_int_2>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. You might want to replace and with or. So, that the loop continues until one of the variable is still greater than 0.

officialaimm
  • 378
  • 4
  • 16
0

Open your python interpreter and try the following,

Let's assume I have a number which has value 2

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

Now let's check if number>=0

>>> number>=0
True

Yes! It is True. Because number is greater than 0. This what you should be doing. Checking whether number>=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. 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.

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

However this doesn't execute because one value is 0. In python you don't have to check if value>=0. The moment a value has 0 in it that itself is considered a false. 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 i.e >=0 loop will run. It will exit only when one of the values is 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
void
  • 2,571
  • 2
  • 20
  • 35
  • This is mostly right, but your final code uses `and` instead of `or`. – user94559 Jun 28 '17 at 04:06
  • I also generally prefer `if foo > 0` to `if foo`. It's harder to make mistakes like accidentally having a string in that variable. "Explicit is better than implicit!" – user94559 Jun 28 '17 at 04:08
  • yeah...`until all values are 0` i missed that thanks man..i'll update. Yours is great solution btw +1 – void Jun 28 '17 at 04:09
  • Actually, you should really use `mystery_int_1 > 0`, not just `mystery_int_1`. Negative numbers are considered truthy in Python. Did you actually run your code? I think your loop will run forever. – user94559 Jun 28 '17 at 04:20
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)
TheEsnSiavashi
  • 1,245
  • 1
  • 14
  • 29