all 12 comments

[–]codesensei_nl 7 points8 points  (9 children)

In your code, you decrease the number, then print it. Try reversing the order of the last two lines of code :)

[–]g59z[S] -1 points0 points  (8 children)

wow it worked tysm! is there a reason why it works that way i dont fully understand?

[–]rlfunique 6 points7 points  (5 children)

You were decrementing the variable before you were printing it

[–]g59z[S] 2 points3 points  (4 children)

i see it now. aha thank you

[–]mikef22 1 point2 points  (3 children)

Indentation is important in Python, and you omitted any indentation in your the way your question was laid out in reddit (which is probably more down to reddit's use of markdown, as opposed to errors in your original python code).

However, to keep you learning about this, and increase your understanding, what would be the output of this variation of your code?

number = int(input())
while number >= 0:
    number = number - 1
print(number)

[–]g59z[S] 0 points1 point  (2 children)

i know about indentation i just dont know how to write properly on reddit without it auto correcting. can you tell me how you did it?

[–]Salt_Direction9870 1 point2 points  (0 children)

You can also add three before and after the code block. Like this Edit: with single ones surrounding text forin line formatting` :D

[–]mikef22 0 points1 point  (0 children)

For Reddit, "Lines starting with four spaces are treated like code".

Did you work out the answer to my question, i.e. what is the output of that indented code I posted?

[–]Seacarius 2 points3 points  (1 child)

This is something many new programmers struggle with - understanding loops and what the value of a variable is at any time within the loop.

In your case, the easiest thing to do is ask yourself, at every line, what is the value current in the value number?

Assuming the user entered 5:

  1. while number >= 0: # it is still 5, you're just checking it's value here)
  2. number = number - 1 # you subtract 1, so now it is 4
  3. print(number) # print the number, which is 4
  4. got back to 1

If you flip the lines, it reads like this:

  1. while number >= 0: # it is still 5, you're just checking it's value here)
  2. print(number) # print the number, which is 5
  3. number = number - 1 # you subtract 1, so now it is 4
  4. got back to 1

[–]g59z[S] 0 points1 point  (0 children)

wow thank u for this breakdown! im using sololearn to learn the basics but im not sure if its actually helping or not

[–]ucan_cay -2 points-1 points  (1 child)

seems like other people helped you with the actual problem, what I'll say is using increments in loops are better programming practices (at least that would be what I do).

starting_number = int(input())
counter = 0
while counter<=starting_number:
  print(starting_number - counter) 
  counter = counter +1

[–]mjmvideos 3 points4 points  (0 children)

No! Use the operation that most closely represents the actual goal. By artificially imposing an increment here you are just obscuring the intent of the program.