For loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

For loop

Print a loop: 0 1 4 9 16 I've got: for i in range (0,5): i += (1) if i % 2 !=0: print(i) Any ideas how to increase each loop by an iteration of an odd number? x + 1, then x + 3, then x + 5, etc? Thanks for any help:)).

5th May 2019, 12:58 AM
tristach605
tristach605 - avatar
8 Answers
+ 4
Diego got it 👍
5th May 2019, 5:18 AM
David Ashton
David Ashton - avatar
+ 1
you could do what i did on this code https://code.sololearn.com/co3k6QKJHU83/?ref=app
5th May 2019, 1:12 AM
Pikachu
Pikachu - avatar
+ 1
for i in range(x): print(i**2)
5th May 2019, 1:24 AM
Diego
Diego - avatar
+ 1
Thanks, Aki:). That does increase each integer by 1. Now, how do I increase each iterarion by an incramentally higher odd number (first + 1, then x + 3, then x+5, etc.)? I'll edit my post now to reflect the actual desired outcome. :).
5th May 2019, 1:24 AM
tristach605
tristach605 - avatar
+ 1
Thanks David and Diego! i've tested it out and it works. One more question: It says: print(i**2) Output: none As (i**2) is any number times itself why isnt 0 •• 2 achknowledged in this code? Thanks for help!
5th May 2019, 4:58 PM
tristach605
tristach605 - avatar
+ 1
Perhaps you included the print statement in an if condition that is never met.The full code would look like this for i in range(5): print(i**2)
5th May 2019, 5:08 PM
Diego
Diego - avatar
0
if so, then maybe this code can help https://code.sololearn.com/c4On2dbkxWd6/?ref=app
5th May 2019, 1:28 AM
Pikachu
Pikachu - avatar
0
Thanks, Aki! That's got the correct iteration. Now; how do we get it to start from zero? x = int(input()) y = 1 for i in range(0,18): print(x) x+=y y+=2
5th May 2019, 1:39 AM
tristach605
tristach605 - avatar