0

I need a pit of help. I am currently learning python, and I have python 2.7.8 I am looking to build a simple program that will help to count the vowels in my word. Here is the code:

count = 0
total = 0
for v in "bonbon":
  count += 1
  if v == 'e' or v == 'o' or v == 'u' or v == 'a':
    print('the number of vowel in your word is ' +str(total))

Why does it print twice? 1- the number of my vowel is 0 and then the number of my is 2

Could someone help me please? Thanks guys

1
  • 1
    For future questions, inspect the toolbar above the text field when you ask/edit a question. There is a code block button marked with {}. If you select your code and click this button it is automatically correctly indented, and displayed correctly in the question. Commented May 28, 2016 at 15:50

1 Answer 1

0

It's printing twice because you have the print inside of the for loop. You should instead increment total inside the for loop and then print it afterwards. If you do:

count = 0
total = 0
for v in "bonbon":
  count += 1
  if v == 'e' or v == 'o' or v == 'u' or v == 'a':
    total += 1

print('the number of vowel in your word is ' + str(total))

It should work.

Sign up to request clarification or add additional context in comments.

3 Comments

I would suggest to check wheter a letter is a vowel or not in a more pythonic way, namely if v in 'aeiouAEIOU':
Thank you so much.Its sorted now
Just don't know how to do it/ dont really use this site

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.