Home > Mobile >  Simple Loops python "for"
Simple Loops python "for"

Time:10-25

I have this program here and I've been instructed to loop the output of this code 50 times.

n = 2
x = 0

for x in range(1, 15):
   print(n)
 n = n   2

I'm new to loops/python 3.6 in general, but how would I loop the output of this code? I'm looking to print the output of this code, 50 times. The code written here is working. I am looking to print out what this code produces, 50 times.

CodePudding user response:

So you can see in the example you've given that the loop is constructed like this:

for x in range(1,15):
    ~~Stuff inside the loop
    ~~More Stuff Inside the loop
~~Now we're out of the loop
print(who do we appreciate)

So the for _ in range() is how many times the loop will execute, then after the colon everything tabbed over will be part of the loop, and once you go back to the same indentation, the loop is over.

so to make this whole thing execute 50 times, we would want to wrap everything inside another for loop.

I hope visualizing it with the indentation helps

CodePudding user response:

This seems to be a programming exercise, rather than real world problem, so I think it is best not to provide a full answer, rather to give just a hint that allows you to find the answer for yourself, that will make you really improve.

If I've understood your question correctly, you should nest your code inside another loop that is repeated 50 times.

CodePudding user response:

Is this what you want :

n=2
x = 0
list = []
for x in range(1,51):
 list.append(n)
 n  = 2
for x in range(1,51):
 print(list)
  • Related