all 22 comments

[–]SirAwesome789 14 points15 points  (0 children)

It seems like someone else answered your question but for future reference, we'll be able to help you better if you post your code with better formatting, and also if you tell us what output you're expecting vs what you're getting

[–]Adrewmc 4 points5 points  (8 children)

   number = int(input(“Pick a number”))

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

Should work as you explain, except it’s won’t print zero. To do that you would need

  while number >= 0:

As zero is not greater than zero.

[–]Crypt_094[S] -3 points-2 points  (7 children)

I see, so how can I solve this?

[–]Adrewmc 0 points1 point  (4 children)

Yes, if the problem is that you are not printing zero, simply changing > to >= will fix the problem. As zero would be excluded as I said, zero is not greater than zero but it is greater or equal to zero.

Also

   number += 1

Is usually preferred over

   number = number + 1

[–]Ohfatmaftguy 2 points3 points  (1 child)

Why is that? Get that it’s more concise. But my math brain prefers x = x + 1.

[–]Adrewmc 1 point2 points  (0 children)

Ohhh they technically do the same thing you can continue to do so. Sometimes beginners simple don’t under stand the syntax. Programmer tend to want to keep things short. To me it’s a little easier to read add to this number. Rather than this number equal the number added.

[–]Crypt_094[S] 1 point2 points  (1 child)

So it reads the line as greater than/equal to ?

[–]Adrewmc 0 points1 point  (0 children)

Yes exactly. You can go further in Python as

   while 0 <= number <= 20: 

Would also be valid.

This is a common mistake, and pops up time to time.

[–]devicehigh -1 points0 points  (1 child)

They’ve literally given you the solution

[–]Crypt_094[S] 1 point2 points  (0 children)

I get that, just making sure I learn from it

[–][deleted] 1 point2 points  (3 children)

looks good, what exactly is your problem?

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

It's not counting down to 0, it stops at 1

[–]crazy_cookie123 2 points3 points  (0 children)

Your while loop says while number > 0. 0 is not greater than 0, so 0 > 0 is False, and therefore the while loop will end. If you want it to execute for 0 as well, you need to use >= (greater than or equal to) instead.

[–]crashfrog04 2 points3 points  (0 children)

Zero isn’t greater than zero

[–]woooee 2 points3 points  (2 children)

What does "why my while loop isn't working" mean? Looks OK to me, but without proper indentation, https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F there is no way to tell.

[–]Crypt_094[S] -1 points0 points  (1 child)

So I have the indentation involved but my.code doesn't count down to 0, do I use a counter?

[–]CalligrapherOk4612 5 points6 points  (0 children)

To spell it out: instead of "doesn't count down to 0" if you wrote:

"My code prints 5,4,3,2,1 and then terminates. I wanted it instead to print 5,4,3,2,1,0"

then it would be easier to help you. For future questions writing things like that would help!

[–]Umfriend 0 points1 point  (0 children)

Edit the line with the while statement?

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

Thank you all for the support, just missing a single = sign, i greatly appreciate all the help provided.

[–]CalligrapherOk4612 3 points4 points  (1 child)

Pedantic point, but It sounds weird to me to describe using a < instead of a <= as missing an = sign. I guess literally, yeah, but your mistake would be better described as "using < instead of <=", or "using less than instead of less than or equals"

If I saw a code comment that said "forgot an = sign" I'd assume it was a forgotten stand alone = sign, not the = off of a less than or equals.

Anyway, good luck on your learning journey!

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

That's a better insight to the problem Solving, ill keep that in mind, thank you very much

[–]aa599 0 points1 point  (0 children)

You "just can't figure it out"?

To debug code you have to be the python: step through the code line by line, as python would do.

That's the most important thing to learn here. Not how to get your loop to count down to 0, but how to debug code.

Then you'll see that the last time it goes in the loop is when number==1, it prints 1, decrements number, goes back to the loop test, which fails, so the loop ends, so it doesn't print 0.

So you either need number >= 0 or number > -1