Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 19 Online
OpenStudy (anonymous):

Hi everybody, I need your help with this question please. Convert the following code into code that uses a for loop. This is what I have so far: while True: print("Hello!") for num in range(10, 0, -2): if num%2== 0: print num I get the right answer below, but it keep repeating it self. What should I put in the statement to stop repeating the answer! Thanks for your help! print "Hello!" print 10 print 8 print 6 print 4 print 2

OpenStudy (anonymous):

Take out line 1 and line 4. Line 1 says will while True is True which will always be True Line 4 just checks if it is a multiple of two but the for loop as written can only make those numbers so the check is not needed. If for some reason you wanted to keep the while loop or just for future reference .. you can use the break statement to break out of a loop. while True: print("Hello!") for num in range(10, 0, -2): print num break

OpenStudy (anonymous):

Thanks very much msmithhnova for helping clarifying this question for me!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!