3 Replies - 230 Views - Last Post: 30 April 2013 - 07:50 PM Rate Topic: -----

#1 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

Print result of FOR Loop not correct

Posted 30 April 2013 - 07:26 PM

I'm doing a learning module on the for loop in python. Here's my code so far:

sentence = 'now is the time for all good people to come to the aid'
count = 0
for letter in sentence:
	if letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' \
 or letter == 'u':
		count = count + 1
		print ('The number of vowels is ' + str(count))


When I run it in my shell, this is my output:

The number of vowels is 1
The number of vowels is 2
The number of vowels is 3
The number of vowels is 4
The number of vowels is 5
The number of vowels is 6
The number of vowels is 7
The number of vowels is 8
The number of vowels is 9
The number of vowels is 10
The number of vowels is 11
The number of vowels is 12
The number of vowels is 13
The number of vowels is 14
The number of vowels is 15
The number of vowels is 16
The number of vowels is 17
The number of vowels is 18
The number of vowels is 19

I cannot figure out what I need to change to only make it print the last line (The number of vowels is 19)
I looked up a couple websites, and others are able to just print the last line; what am I missing?

This post has been edited by pyr0b0y: 30 April 2013 - 07:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Print result of FOR Loop not correct

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Print result of FOR Loop not correct

Posted 30 April 2013 - 07:33 PM

Your print line should not be within the for loop body. Also you should really use in rather than painfully chaining those conditionals with or.

sentence = "Now is the time for all good people to come to the aid"
count = 0
for letter in sentence:
    if letter.lower() in "aeiou":
		count += 1
print("The number of vowels is {}".format(count))

-Mek

This post has been edited by Mekire: 30 April 2013 - 07:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

Re: Print result of FOR Loop not correct

Posted 30 April 2013 - 07:45 PM

thanks, that seemed to work, the course im taking uses [or] so im not familiar with [in] yet.

This post has been edited by pyr0b0y: 30 April 2013 - 07:45 PM

Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Print result of FOR Loop not correct

Posted 30 April 2013 - 07:50 PM

pyr0b0y said:

thanks, that seemed to work, the course im taking uses [or] so im not familiar with [in] yet.

Yeah people always seem to think or and and should be taught before in. I personally think they should be taught concurrently.

Just keep in mind that whenever you have to do something that looks like this:
if letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' or letter == 'u':
there is usually a better way.

-Mek
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1