Maker Pro
Maker Pro

Question on use of Python language

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
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
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
You have a nested loop it appears... although it's hard to tell, it feels like it's missing punctuation or additional formatting...

The for loop will loop as many times as there are letters in the string provided to it...
Count is actually being incremented by 1, 12 times each time the outer While loop is run, it's not merely doing count +12.
The print and interation += 1 seems to lie outside of the for loop, and only runs once each time the outer While loop is run.

Perhaps you need to see if you are missing a pair of brackets or a keyword to define where the code's For loop ends.
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Code:
Does EP have a code tag?
   perhaps.
      great!
   perhaps not.
      no so great.
Return

If you can please edit your first post, or make a new post, but put all of your Python code within the BBCode 'code' tags
Code:
[code]
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Code:
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
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Thanks, this is how it appears in the problem set (output below). Don't worry, this is after posting the answer, that is why I didn't post it in homework.
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Code:
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
Ok so I found this : http://www.diveintopython.net/getting_to_know_python/indenting_code.html
Talks about the lack of curly braces and such to denote the end of loops.
If you are unfamiliar, the for loop you have ill only run the line directly below it, and that's it...
You can try to indent the count+=1, print, and iteration+=1 line one more time, and it should end up printing 60 output lines for you ;)

Iteration 0; count is: 1
Iteration 0; count is: 2
Iteration 0; count is: ...
Iteration 0; count is: 11
Iteration 0; count is: 12
Iteration 1; count is: 13
Iteration 1; count is: 14
Iteration 1; count is: ...
Iteration 1; count is: 23
Iteration 1; count is: 24
etc.
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
I thought Python looked more relaxed in it's syntax! I did find an error in how I originally posted it - it might make more sense to you now, however, the output is still the same: (I think the forum edited out the white space again)

Code:
iteration = 0
count = 0
while iteration < 5:
       for letter in "hello, world":
              count += 1
       print "Iteration " + str(iteration) + "; count is: " + str(count)
       iteration += 1

I see it as at the start iteration is zero, something happens with "letter in" (not sure what goes on there, whether its a keyword in python or a variable), count is incremented (original count plus one, next time, existing count plus one) then print variables iteration and count, iteration incremented by one, while conditional tested until it hits 5 (so the entire piece goes around 5 times).
 
Last edited:

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
I thought Python looked more relaxed in it's syntax! I did find an error in how I originally posted it - it might make more sense to you now, however, the output is still the same: (I think the forum edited out the white space again)

Code:
iteration = 0
count = 0
while iteration < 5:
       for letter in "hello, world":
              count += 1
       print "Iteration " + str(iteration) + "; count is: " + str(count)
       iteration += 1

I see it as at the start iteration is zero, something happens with "letter in" (not sure what goes on there, whether its a keyword in python or a variable), count is incremented (original count plus one, next time, existing count plus one) then print variables iteration and count, iteration incremented by one, while conditional tested until it hits 5 (so the entire piece goes around 5 times).
Got another link for you ;)
http://www.tutorialspoint.com/python/python_for_loop.htm

Code:
While iteration < 5:
   For letter in "Hello chopnhack!":
      count += 1
      print "Running for Letter " + letter
   print "Iteration " + str(iteration) + "; count is: " + str(count)
   iteration += 1
So... because 'count+=1' is the only indented item under the 'For' , that is the only code that loops with the 'for' loop.
The keywords here you need to know is the 'For' and the 'in'. 'letter' is simply a variable.
When For is used with the 'in' keyword, it will loop through each entry in an array. "Hello, world" happens to be an array with 12 elements.
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Yes the forum software limits whitespace to two spaces whenever you paste text. You have to manually fix the indentation after you've pasted it. It's a total pain. Even if you paste it inside a CODE block, the whitespace stripping behaviour is the same.

With the code indented as you have in post #7 I would expect the output you posted in post #4.

The "for letter" loop, which just increments count, is executed for every character in the text string. The actual letter variable, which contains the character each time through the loop, isn't used; the loop simply counts the characters.

The outer loop goes through five iterations. on each iteration, the count variable is updated by adding the length of the string (12 characters), then it prints the iteration number and the value of count.
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Thanks guys! I figured where the 12 was coming from, but the syntax of the for/in loop had me loopy. Thanks @Gryd3 - that link looks like it will be very useful!!
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Code:
school = 'Massachusetts Institute of Technology'
numVowels = 0
numCons = 0

for char in school:
     if char == 'a' or char == 'e' or char == 'i' \
        or char == 'o' or char == 'u':
           numVowels += 1
     elif char == 'o' or char == 'M':
           print char
     else:
           numCons -= 1

print 'numVowels is: ' + str(numVowels)
print 'numCons is: ' + str(numCons)

  1. How many times does o print out? Disregard the o's in last two print statements.

    0 - correct

  2. How many times does M print out?

    1 - correct

  3. What will the value of the variable numVowels be?

    11 - correct

  4. What will the value of the variable numCons be?
    25 - correct
Today's question centers on question 1. - The answer shows as 0 being correct, but when I looked over the code I couldn't understand why 'o' did not print out. In the first section of code we tested for vowels - 'o' being True, variable numVowel increments by one. Doesn't the program pass control to the next statement? If so, then elif char == 'o' equals True and print char should print out an 'o'. What is wrong with my logic here?
Thanks!!
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Code:
 1 school = 'Massachusetts Institute of Technology'
 2 numVowels = 0
 3 numCons = 0
 4
 5 for char in school:
 6      if char == 'a' or char == 'e' or char == 'i' \
 7         or char == 'o' or char == 'u':
 8            numVowels += 1
 9      elif char == 'o' or char == 'M':
10            print char
11      else:
12            numCons -= 1
13
14 print 'numVowels is: ' + str(numVowels)
15 print 'numCons is: ' + str(numCons)

Line 12 should read numCons += 1

The elif at line 9 means "else, if ...". It ties in with the if on line 6. If char matches a vowel, the if statement's condition will be true, so line 8 will be executed, and any else or elif clause will be ignored; that's what else means. The program will execute either the statement after the if, or the statement after the else, not both.

When char is 'M', line 8 is not executed, so the else is executed and char is compared to 'o' and 'M' and it matches 'M', so line 10 is executed.
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Line 12 should read numCons += 1
They did that on purpose to see who was paying attention to the sign of the value. Typical teacher trick, eh?


The elif at line 9 means "else, if ...". It ties in with the if on line 6. If char matches a vowel, the if statement's condition will be true, so line 8 will be executed, and any else or elif clause will be ignored; that's what else means. The program will execute either the statement after the if, or the statement after the else, not both.
Thanks Kris! - I understand it now :) I was getting confused on the order and it makes more sense now. They throw terms out and sometimes they are not fully developed, thank God this is a beginners course!!! LOL
 
Top