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!
n = 10000
while (n > 0):
n = n - 100
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.
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!
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?
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.
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.
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.
n = 10000
while n > 0:
decr = 100
for i in range(decr, 10, -1):
n -= decr
n = 0
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
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:
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.
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.