I’m just starting to learn while loops.
I’m trying to iterate same number 10 times using a while loop.
I figured out how I can utilize while loop in order to stop at a certain point as it increases
But I can’t figure out how to stop at a certain point without having to add 1 and setting a limit.
Here is my code
i = 1
total = 0
while i < 11:
print i
i += 1
total = i + total
print total
This prints
1,2,3,4,5,6,7,8,9,10,65
in a separate line
How could I modify this result?
1,1,1,1,1,1,1,1,1,1,10?
Just print a literal
1and add1to the total:You need to keep track of how many times your loop is run, and using
ifor that is fine, but that does mean you need to increment it on each run through.If, during each loop, you only want to add one to the total, just do that and don’t use the loop counter for that.