2

I am 15 and currently doing a GCSE in Computing. My knowledge is very basic and I've had a bit of trouble with a piece of code I have to write for a 'Vowel Worth Calculator' which is supposed to check through a word and give it a vowel score depending on how many and which vowels it has. I keep getting an error, and I am completely stumped, any help would be appreciated. Here is my source code :

Vowel Worth Counter

print('Welcome to the Vowel Worth Counter!')

word = input('Please input your word, in lower-case, or type Q to quit.')

if word == 'Q' :
    quit()

def vowelcount(word) :
    lettercount = int(len(word))
    vowelscore = 0
    checkcount = 1
    position = 0
    while lettercount != checkcount :
        if word[position] == str('a') :
            vowelscore = vowelscore + 5
        if word[position] == str('e') :
            vowelscore = vowelscore + 4
        if word[position] == str('i') :
            vowelscore = vowelscore + 5
        if word[position] == str('o') :
            vowelscore = vowelscore + 5
        if word[position] == str('u') :
            vowelscore = vowelscore + 5
        position = position + 1
    if lettercount == checkcount :
        print('I have finished calculatiing your Vowel Score.')
        print('Your Vowel score is ' + str(vowelscore) + '!')
        for x in range (0,1) :
            break
vowelcount(word)

As I said, any help would be appreciated, thank you.

3
  • 1
    You never change checkcount, so your while loop can never end. Commented Nov 20, 2013 at 22:01
  • while lettercount != checkcount. You never alter either in your while loop, while you still increment position. This is why you're code is dying Commented Nov 20, 2013 at 22:02
  • Also, in Python it is far more usual to loop directly over data structures like lists or strings than it is to use an index. for letter in word: would be the Pythonic idiom, at least if I weren't going to use more advanced constructions. Commented Nov 20, 2013 at 22:02

2 Answers 2

3

The exit contidion in the loop should be:

while position < lettercount:

Or even simpler, you can iterate over each character in a string like this:

for c in word:
    if c == 'a':
        # and so on
Sign up to request clarification or add additional context in comments.

Comments

2

It might be more pythonic to use the dictionary data structure

vowels = { 'a' : 5, 'e' : 4, 'i' : 5, 'o' : 5, 'u' : 5}

vowelscore = 0

for letter in word:
    if letter in vowels:
        vowelscore += vowels[letter]

4 Comments

Haha! I had written the exact same code, but had resisted from answering the question, because I thought it was a little too cocky. Plus, it did not answer the question the OP asked, "Why his code is not working?". Anyway! :)
Yeah. I figured since @ÓscarLópez had already answered the OP, I figured I would suggest looking at a data structure he/shre might not have encountered before :). That's funny though
OK, since we're getting into alternate constructions - define the dict as above, then vowelscore = sum(vowels.get(letter, 0) for letter in word)
Thank you all so much, was not expecting a response!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.