+ 1

How does "nested for" work on python?

i just found this code and i don't understand the "nested for" worked (sorry for my bad english i hope you guys understand) https://code.sololearn.com/c8auqmenLbSE/?ref=app

4th Mar 2018, 10:51 AM
Solehudin
Solehudin - avatar
7 Antworten
+ 7
hi, with one more print in your code it becomes clear https://code.sololearn.com/c4t5S8KgPFh8/?ref=app
4th Mar 2018, 11:08 AM
Oma Falk
Oma Falk - avatar
+ 5
whenever the outer loop (i ) counts up, the variable is set to zero. for checking the next number to prime: all other variables backt to their positions. okay?
5th Mar 2018, 5:16 AM
Oma Falk
Oma Falk - avatar
+ 5
you need a short docu for the prog. can you do it on your own? Shall we check before your teacher? --> post it.
5th Mar 2018, 5:19 AM
Oma Falk
Oma Falk - avatar
+ 2
mantapp.... jadi gini: loop inside loop. lets call the parent loop is A (5 times) and the child is B (10 times). when first loop in A, the B is must finish it (10 times) and so on. so, if the B is print('hey') then its gonna print('hey') 50 times. but the code is mess. should be: for i in range(1, 10): if ((i%2)and(i%3)!=0): print(i, "adalah prima") else: print(i, 'bukan prima')
4th Mar 2018, 11:10 AM
Kevin AS
Kevin AS - avatar
0
how about the variable between both "for"? why does the value change?
4th Mar 2018, 11:21 PM
Solehudin
Solehudin - avatar
0
okay. thanks for the explanation guys.
5th Mar 2018, 7:24 AM
Solehudin
Solehudin - avatar
0
just keep practice until you understand 🤗
5th Mar 2018, 7:50 AM
Kevin AS
Kevin AS - avatar