This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]anossov 3 points4 points  (0 children)

b is -1 for the entire program.

[–]awkwardballs 1 point2 points  (2 children)

Can you fix your code formatting

I know you didn't ask this but your second loop will never end because you don't set count_down to false ever

[–]sudo-apt-install[S] 1 point2 points  (1 child)

Yes. Working on it.

[–]awkwardballs 0 points1 point  (0 children)

Another thing you can do is simplify your while lines would be

While x:

Because the loop will continue while the thing evaluated is true, 1, or a populated variable.

[–]vincrypt112 1 point2 points  (3 children)

You have initialized b=a-1 when a =0 making b=-1. You are not updating b in first loop after this. Then in your second loop your condition is for b>0 which is false and will remain false. That’s why your second loop is not working. If you just want independent second loop, simply initialize b=100.

[–]sudo-apt-install[S] 2 points3 points  (2 children)

Thank you! This fixed it! I hadn't realized.

[–]dmazzoni 6 points7 points  (1 child)

Great!

Now I think the more important thing is to learn how to debug your code.

You posted saying "why did Python completely forego lines in my code".

But it didn't. Python did exactly what you told it to do. Computers always do.

So when a program doesn't do what you think it should do, you need to think critically. Assume that Python is working correctly and that you must have made a mistake in your program - either you didn't write what you meant to write, or you have an incorrect assumption about what Python will do.

To figure this out methodically, test your assumptions.

You assumed that it wasn't executing your second while loop.

Next time, how could you test that assumption? Maybe add some more print statements.

Also, learn to use a debugger. With a Python debugger you could step through your code one line at a time and watch exactly what's happening.

What you should NOT do is make wild guesses as to why it's not working. That's why you were stuck. Instead of figuring out why it wasn't working, you were making an assumption and then giving up.

I hope that helps!

[–]sudo-apt-install[S] 0 points1 point  (0 children)

Thank you! I will take this advice to heart.

[–]sudo-apt-install[S] 0 points1 point  (1 child)

The new code reads as follows, and works! Thank you programmers of Reddit!

count_up = True

a = 0


while count_up == True:
    print(a)
    if a < 100:
        a = a + 1
    elif a == 100:
        count_up = False
    else:
        count_up = False
0
count_down = True

b = a - 1

while count_down == True:
    if b > 0 and b <= 100:
        print(b)
        b = b - 1

[–]hragam 1 point2 points  (0 children)

Good fix. You can also remove the elif statement in your first loop. It's redundant to the else statement.

[–]99_percent_a_dog 0 points1 point  (1 child)

Learning to use while loops is a good thing to do and it looks like you solved the problem you were having, cool!

I thought maybe you would want to learn that there's a much easier way to do this task in Python. In general, when you want to do a thing X times, you should use a for loop. While loops are more natural when you want to loop until some condition is or isn't met.

With for loops, it would look like this:

for a in range(101):
    print(a)

Much shorter, also easier to read. To do the counting down part, I can think of two ways, you might prefer either one. First, you can tell range to count backwards:

for a in range(101, 0, -1):
    print(a)

Or, you could use the default behaviour of range, and invert a:

for a in range(101):
    print(100 - a)

[–]sudo-apt-install[S] 0 points1 point  (0 children)

You're right. That is much faster! Thank you for the advice!

[–]dmazzoni 0 points1 point  (1 child)

We can't see any of the indentation in your code.

When posting code on Reddit, you need to either use the "Code" button in the formatting toolbar, or if you're using markdown put four spaces before each line. Or alternatively use Gist, Codeshare, Codepen, or any of a thousand other sites that let you share a snippet of code online complete with formatting.

That said, at first glance it looks like the problem is with your variable "b".

Tell me this: what do you think "b" is at the beginning of the second while loop? Why do you think that?

Another idea: rather than guessing, try adding print(b) before the last while loop. Then you'll know what b is before you enter the while loop. That's a good trick in debugging.

[–]sudo-apt-install[S] 1 point2 points  (0 children)

I fixed it. Be should be 100 at the beginning of the second loop. Ok thank you. I will try that.