Any for loop exercises out there? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Any for loop exercises out there?

I'm having difficulties understanding the for loop, even after reviewing the lesson and browsing the internet. So can anyone please give any exercises (like codes that uses the for loop effectively)?

23rd Mar 2017, 1:27 AM
Francis Santos
Francis Santos - avatar
2 Answers
+ 14
Summing numbers in a range is a common for loop. For example, what is the sum of all integers from 1 to 100?
23rd Mar 2017, 3:39 AM
Jafca
Jafca - avatar
+ 1
try this: import random k3 = 0 for i in range(0, 50): k4 = random.randint(0,100) k = 10 while(k >= 0): k2 = random.randint(0, 3) if(k2 == 0): k3 += k / random.randint(1, 4) k -= 1 elif(k2 == 1): k3 += k * random.randint(1, 4) k -= 1 elif(k2 == 2): k3 += k / random.randint(1, 4) k -= 1 else: k3 += k - random.randint(1, 4) k -= 1 print(k3)
23rd Mar 2017, 4:55 AM
sniperisdemoman
sniperisdemoman - avatar