+ 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)?

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?

+ 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)