While loop count down from 10000?

Hello Everybody,

Sorry to ask but have an issue trying to figure out using a while loop from 10000. My biggest issue is each iteration of the loop must decrease the number by a value of 100 thru 10. What does thru 10 mean, value of 100 is easy but not understanding something? What I have is below along with the start of the output. Thanks for any explanation, just learning Python!

Kent

n = 10000
while (n > 0):
    print(n)
    n = n - 100
print("Done") 
10000
9900
9801
9703
9606
1 Like

When we say ‘one thru ten’ that means the same as ‘one to ten, inclusive’. Likewise ‘10 thru 100 by 10’ could be interpreted as,

10 20 30 40 50 60 70 80 90 100
>>> n = 10000
>>> while n > 0:
	print (n)
	n = n - 1000

	
10000
9000
8000
7000
6000
5000
4000
3000
2000
1000
>>> 

This may be sketchy, given we don’t know the exact problem. Is this an exercise question?

2 Likes

Yes it is, only worth 4 points so not worried if I don’t get it. Problem is define a variable of 10000, use while loop to count down until it reaches 0, I got that, then it says each loop must decrease the number by a value of 100 thru 10 until it reaches 0. This is where I’m stumped, it says that the first few numbers displayed should be.

10000
9900
9801
9703
9606

I’ve not had any math class in 38 years but that is not decreasing by 100 so I’m missing something?
Thanks for your help!

1 Like

Sorry, I got called away. Standby for a reply from @appylpye.

2 Likes

Hi @skentback,

Without a precise description of the problem, we cannot ensure that our advice will be effective. Have you quoted the problem description exactly?

Also, when you post code, it should be formatted so that we can see details such as indentation. See How to ask good questions (and get good answers) for advice.

It appears from what you provided regarding the problem description and the sample output, that the problem asks that you decrease the value of the variable by 100 the first time through the loop, then by 99 the next time, and by 98 the time after that. You keep following that pattern until you eventually decrease the value of the variable by 10. At that point, the value of the variable will be 4995, which is greater than 0, so you need to continue the decrease its value. So you again decrease it by 100, then by 99, and then by 98, and continue the pattern. Eventually, the value of the variable reaches 0, and you are done. Is this what you are being asked to do? If you are not sure, could you quote the problem description exactly as it was given to you?

3 Likes

Thanks for the help, I’ve copied it below. I do see the pattern now but not sure how they got it.

*define a variable and set it to the integer number of 10000
  • use a while loop to count down this number until it reaches 0
  • each iteration of the loop must decrease the number by a value of 100 thru 10, repeat this if we have not reached zero
    
  • display each new value of the variable
    
  • when the variable reaches zero or goes below zero the program will end
    
  • never let a negative value display to the user
    

The first few numbers displayed should be:

10000
9900
9801
9703
9606

The last few numbers displayed should be:
65
50
36
23
11
0

2 Likes

Have you learned about nested loops? That would consist of a loop within a loop, and would be useful for this problem.

1 Like

Just started today the Loops on Code Academy and as I said after you showed me the pattern that I wasn’t seeing, the class I’m taking is just an intro to computer programming and its not very good but I’m just doing this for me not a job.

Thanks,
Kent

1 Like

Your while loop can serve as the outer block of a nested loop. These lines of code, which you already have, form a fine beginning:

n = 10000
while (n > 0):

Within the while loop, you need to define and modify a variable that will represent the number by which you will be decreasing n. Let’s name that variable decr. Since it will start at 100 and decrease to 10 by 1 during each iteration, a for loop would be a perfect fit for an inner loop here. Within that for loop, you would need to subtract decr from n, output the value of n, then check whether n had reached 0. When n reaches 0, you need to break out of the loop and terminate execution of the program.

You can give this a try and post you code for further discussion.

1 Like

Thank you VERY much, I should have tried to learn this earlier than 57 years old!

Kent

3 Likes

Well I’m not understanding how to decrease by 100 and by 1, I’ve tried a lot of different configurations. I’ve spent hours reading trying to figure out what I’m missing, can I ask for a little more he, below is what I have.

image

Thanks,
Kent

n = 10000

while n > 0:
    decr = 100
    for i in range(decr, 10, -1):
        print(i)
        n -= decr
    n = 0
    break
1 Like

In the for loop, decr needs to start at 100 then decrease to 10 by 1 during each iteration. Accordingly, the header of that loop should be:

  for decr in range(100, 9, -1):

After the loop, you currently have these final two lines:

    n = 0
    break

Instead, you need to test the value of n, and execute the break if it equals 0. Just in case you wish to experiment by modifying the code later on to initiate n to a value other than 10000, which might cause n to ultimately go negative instead of its reaching 0 exactly, it may be better to test whether n is 0 or negative. So, the final two lines that you have now should be changed to:

        if n <= 0:
            break

Note that those lines need to be contained within the for loop.

You do not need the variable, i.

Try the above, let us know how it works out, and post your code again, if necessary.

This post was modified on July 31, 2019 to note that the testing of the value of n and the related break need to be part of the for loop.

1 Like

Thank you very much, that worked, I almost had that last night, but mine would decrease by 100 then decrease by 1. I really appreciate it, maybe buy the time I get thru with all the books and code academy I’ll understand this better.

Kent

2 Likes

A post was split to a new topic: Can you share your final code?