-4

Write a python program that asks the user for a word, then calculates and prints the vowel worth of the entered word using these instructions: Suppose you calculate the vowel worth of a word based on the following instructions:

a   5 points
e   4 points
i   3 points
o   2 points
u   1 point.

My code:

word = str(input("Enter a word:"))

def vowel(Word):
    global word
    Word = word
    score = 1

    if "a" or "A" in word:
        score += 5
    elif "e" or "E" in word:
        score += 4
    elif "i" or "I" in word:
        score += 3
    elif "o" or "O" in word:
        score += 2
    elif "u" or "U" in word:
        score += 1

    print("Your word scored",score,"in the vowel checker")

print(vowel(word))    

Edit: FOR LOOP

word = input("Enter a word:")

def vowel(Wo_rd): global word Wo_rd = word score = 0 for char in word.lower(): if char == 'a' or "A": score += 5 elif char == "e" or "E": score += 4 elif char == "i" or "I": score += 3 elif char == "o" or "O": score += 2 elif char == "u" or "U": score += 1 a = "Your word scored",score," in the word checker test" return a

print(vowel(word))

3 Answers3

3
word = str(input("Enter a word:"))

def vowel(word):
    score = 1

    for character in word:
        character = character.lower()
        if character == 'a':
            score += 5
        elif character == 'e':
            score += 4
        elif character == 'i':
            score += 3
        elif character == 'o':
            score += 2
        elif character == 'u':
            score += 1

    print("Your word scored",score,"in the vowel checker")

vowel(word)

Things to note:

  • In Python, strings are iterable, so you can loop over each character.
  • Note that you don't have to (and shouldn't) use a global.
  • The use of character.lower() simplifies our conditionals.
  • Instead of printing the output in the vowel function, you could instead just return score and put the print statement in the last line.

P.S. Given the question, shouldn't a word's score start at 0, not 1?

depperm
  • 10,606
  • 4
  • 43
  • 67
pbaranay
  • 585
  • 5
  • 17
  • So does your example include upper and lower case letters , or does it only accept lower case letters, And yes you are correct in saying that the score should be 0 – JackAnderson Jan 16 '17 at 16:56
  • @JackAnderson My example works for both upper- and lower-case letters. `character = character.lower()` turns each letter into lowercase, which simplifies our conditionals. This allows us to have to check against "a", not "a" or "A" like in the original code. – pbaranay Jan 16 '17 at 16:58
  • 1
    It's more efficient to use `for character in word.lower():` because you only call `lower()` once that way. Of course there are other ways to improve efficiency too – Chris_Rands Jan 16 '17 at 17:05
1

First if you are passing word to vowel I don't know why you use global. You are calling vowel inside a print so vowel should return a string instead of printing a string itself. Next by using a for loop you can check each character of the word and increment the score even if multiple occurances of a vowel appear.

word = str(input("Enter a word:"))

def vowel(word):
  score = 1
  for c in word:
    if "a" == c.lower():
        score += 5
    elif "e" == c.lower():
        score += 4
    elif "i" == c.lower():
        score += 3
    elif "o" == c.lower():
        score += 2
    elif "u" == c.lower():
        score += 1

  return "Your word scored "+str(score)+" in the vowel checker"

print(vowel(word))  
depperm
  • 10,606
  • 4
  • 43
  • 67
0

Time complexity is important

for character in word:
    character = character.lower()
    if  re.match("a",character):
        score += 5
    elif re.match("e",character):
        score += 4
    elif re.match("i",character):
        score += 3
    elif re.match("o",character):
        score += 2
    elif re.match("u",character):
        score += 1

print("Your word scored",score,"in the vowel checker")

Enter a word:hello

pbaranay code, Your word scored 7 in the vowel checker

--- 2.4024055004119873 seconds ---

My code,Your word scored 7 in the vowel checker

--- 0.004721164703369141 seconds ---