1

Here are my instructions:

We will pass you a value, N. You should output every positive value from N down to and including 0.

And here is my assignment:

    # Get N from the command line
    import sys
    N = int(sys.argv[1])

    # Your code goes here
    counter = 0 
    while counter <= N:
     print(counter-N)
     counter = counter + 1

My solution prints this:

Program Output

Program Failed for Input: 3
Expected Output: 3
2
1
0
Your Program Output: -3
-2
-1
0

As you can see, I got the output to show -3, -2, -1, 0, but it should be 3, 2, 1, 0. Please be mindful that I am a beginner, and my code must pass using a 'while' statement, and my code must be inputted in Codio. Thank you in advance for any assistance rendered.

cs95
  • 379,657
  • 97
  • 704
  • 746
T.Mic
  • 9
  • 3
  • 6

3 Answers3

2

Since we are expecting N's value to be higher than counter's value, counter-N will be negative.

Try the following:

 import sys
    N = int(sys.argv[1])

    # Your code goes here
    counter = 0 
    while counter <= N:
     print(N-counter)
     counter = counter + 1
li_
  • 124
  • 1
  • 9
  • 1
    Thanks bee it worked. I really appreciate this community because being a student, you don't get much help, and I know the objective is to learn, but if I can't figure something out after a few days, what am I supposed to do? Anyway thanks again!!! – T.Mic Nov 14 '17 at 21:44
1

Just step down from N to 0:

# Get N from the command line
import sys
N = int(sys.argv[1])

# Your code goes here
counter = N
while counter >= 0:
 print(counter)
 counter -= 1
0

Just pass counter-N to the abs function:

while counter <= N:
  print(abs(counter-N))
  counter = counter + 1

Or, just use N-counter:

while counter <= N:
 print(N-counter)
 counter = counter + 1
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • why do you use abs? – Snow Nov 14 '17 at 19:44
  • @cᴏʟᴅsᴘᴇᴇᴅ seeing as the OP is a programming beginner, it makes sense to provide simple workarounds that are more mathematically understandable such as the absolute value rather than tweaking the counter. – Ajax1234 Nov 14 '17 at 19:47
  • 2
    I hardly think introducing a new function is "mathematically simpler" to just reversing the order of operands to get rid of the negative sign. ... – cs95 Nov 14 '17 at 19:48
  • @cᴏʟᴅsᴘᴇᴇᴅ I believe that in this case, the absolute value is more intuitive, whereas `N-counter` is more advanced. – Ajax1234 Nov 14 '17 at 19:51
  • N is (presumably) a positive value. `counter` starts from 0 and goes up to N. So it would "intuitively" make sense to subtract the smaller number from the larger. – cs95 Nov 14 '17 at 19:53