2

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

print("who do we appreciate")

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.

4
  • 1
    so what exactly should your result be? Are you saying loop exactly what you have there 50 times or change your loop that you have to run 50 times? Because you have 2 answers here that answer each one of these questions. So which is it? Commented Feb 22, 2017 at 3:42
  • See how important it is to be clear on your question. Now your getting answers that are changing your increment values. I suggest you edit your post to specify in a clearer way. Commented Feb 22, 2017 at 3:45
  • Thanks for commenting. The goal here is for the output of this code, which is a list of even numbers, to be repeated 50 times. So the desired output is a list of numbers, 2-28 followed by the sentence "who do we appreciate." My assignment is a cheer chant. Commented Feb 22, 2017 at 3:49
  • I have placed an answer with to help you based on your clarifications. No one else did that and just started answering questions. Make sure next time you ask your question that you make it as clear as possible. But on a plus note you got some good information on loops here. Commented Feb 22, 2017 at 3:58

6 Answers 6

1

Based on your clarifications in your comment, then you want this

n = 2



for j in range(0,50):

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

    n = 2
    print("who do we appreciate")

You need to reset the value of n back to 2 as shown above and then it will work the way you have specified

Sign up to request clarification or add additional context in comments.

7 Comments

This code produces an entirely different output. This is what it should look like: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 who do we appreciate? But this is to be printed 50 more times.
I fixed the space issues I had. But I am running this code and that is exactly what it is doing
@jacksonhasflo try it again, this is what it does.
@RSon1234 there is no need to initialize x to 0 . Also it is not a recommended way to use same variable for inner and outer loops
Ok I had some spacing issues of my own, but it's all clear now. I greatly appreciate your help, you have taught me more than my costly college education has :)
|
0

Try using a while loop:

n=0
while n<51:
    print n
    n=n+2

The difference between for and while is that while iterates until some condition is met, in this case we pass 50. The first value is printed, then two are added and it returns to the top of the loop. Not all languages have a while loop but it is a great thing!

Comments

0

do you mean something like this?

n=2

for i in range(0,50):
    for x in range(1,15):
        print(n)
    n=n+2
    print("who do we appreciate")

2 Comments

Write a function and call it 50 times in a for loop.
Definitely not an answer
0

I would do something with a while loop. If you do something like

while n<50: 
   print('who do we appreciate')
   n=n+1

this will print the out put through every iteration of the loop and you can control how many times with the while loop. since it adds 1 to n every iteration it basically acts a counter and when you go through the 50th iteration n will no longer be < 50 so the loop will no longer continue.

Comments

0

I think your code has a simple indentation problem. It works if you change thus:

n = 2
x = 0

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

print("who do we appreciate")

For looping 50 times you have to use 50 instead of 15 in the range. Hope this helps

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you want to loop the entire code 50 times, you need nested loops

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

print("who do we appreciate")

1 Comment

Thank you Prashanth for your answer. However, what I'm trying to loop is the output of this code. So, the desired output would be the output of the above program, times 50.
0

You might want to initialize n inside for loop

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.