I enrolled in MIT's Intro to CS and Python Programming to help me better understand general computational thinking. I was 1/2 way through week 2 until I hit a snag that I haven't been able to pick apart. The for <> in <> loop has got me confused. Here is a sample:
iteration = 0
count = 0
while iteration < 5:
for letter in "hello, world":
count += 1
print "Iteration " + str(iteration) + "; count is: " + str(count)
iteration += 1
Iteration 0; count is: 12
Iteration 1; count is: 24
Iteration 2; count is: 36
Iteration 3; count is: 48
Iteration 4; count is: 60
I didn't understand how the iteration variable got incremented arithmetically, while the count variable got the numerical equivalent of "hello, world" and it was multiplied!
Can anyone help explain how the "for in" conditional works?
Thanks
iteration = 0
count = 0
while iteration < 5:
for letter in "hello, world":
count += 1
print "Iteration " + str(iteration) + "; count is: " + str(count)
iteration += 1
Iteration 0; count is: 12
Iteration 1; count is: 24
Iteration 2; count is: 36
Iteration 3; count is: 48
Iteration 4; count is: 60
I didn't understand how the iteration variable got incremented arithmetically, while the count variable got the numerical equivalent of "hello, world" and it was multiplied!
Can anyone help explain how the "for in" conditional works?
Thanks