0

I have a simple task that I need to do; let's say I have a word, any word. And in that word, I need to find how many instances of a letter there are. And that letter that I'm finding has a value, let's say 5. I then need to multiply how many instances of that letter there are by 5, to accumulate a total score. Sounds simple enough, but I lack the knowledge in order to make it happen. I've begun coding a test, but even that doesn't work. Any help would be appreciated.

def vowel1(Word,x):
    x = a
    for a in Word:
        if a==x:
            print ("True")
        print ("False")

Word = input("Please enter a word (from the english dictionary) if you please ")
print (Word)    

vowel1(Word,x)

I know there are no signs of a variable with value in that, but I don't know how to do it.

The White Wolf
  • 65
  • 1
  • 2
  • 8
  • Seems you need a function to get the value of a letter? This part is not visible in your approach yet. – Wolf Oct 21 '15 at 19:22
  • Possible duplicate of [Count occurrence of a character in a string](http://stackoverflow.com/questions/1155617/count-occurrence-of-a-character-in-a-string) – Wolf Oct 21 '15 at 19:25
  • 1
    The function prototype would need to be something like `score(word, letter, weight)` and it would simply `return weight * sum(1 for c in word if c==letter)`. – Chad S. Oct 21 '15 at 19:25

4 Answers4

1

So, effectively, scrabble?

A nice simply approach would be to create a dictionary of scores and simply go through the word, looking for values in the dictionary. You can add verification and simplify it, but the general logic could be (assuming python3):

import string

def getScore(word, scoremap):
    total = 0
    for a in word:
        total += scoremap[a.lower()]
    return total

word = input("Please enter a word (from the english dictionary) if you please ")
print(word)


scoremap = {}

# generate the scores how you wish, the following is just an example:
for i in string.ascii_lowercase:
    scoremap[i] = 1

scoremap['e'] = 5

print(getScore(word,scoremap))
Jake
  • 822
  • 5
  • 14
  • That's awesome stuff; I really like that. Thank you :D I have one more question; let's say I have multiple scores, how do I get to accumulative score? – The White Wolf Oct 25 '15 at 20:26
  • Alas, I have discovered a simpler way to do it; I'll submit it as a solution. Yours works perfectly, but in all honesty [I have no idea what you've done]; therefore, there's no point using it if I can't understand it. I'll give it a thumbs up though :D – The White Wolf Oct 25 '15 at 20:28
  • Well that's the point of the scoremap. Set each a value of 0 by default, and give some letters a score with `scoremap['a'] = 4` etc. This automatically calculates the score, and is much more general than your solution. – Jake Oct 25 '15 at 21:41
  • So the way it works is as follows: you define a dictionary which maps characters to values. You then iterate through the characters in your word and add their corresponding value (to lower incase there are capitals there) to a running total. – Jake Oct 25 '15 at 21:52
0

I think you have a small mistake. Remove the second line of your function?

def vowel1(Word,x):
    # x = a  # This negates the value of x you just entered in the function
    for a in Word:
        if a==x:
            print ("True")
        print ("False")
Red Twoon
  • 685
  • 4
  • 14
  • Sorry for getting back late; I tried this, however it still doesn't work coming up with the error that x has no value. – The White Wolf Oct 25 '15 at 18:37
  • You have to enter a string, ("a", for example) because x hasn't been defined. vowel1("apples", "a") would print 1 true and 5 falses. – Red Twoon Oct 26 '15 at 21:37
0

str.count(sub[, start[, end]])

Example:

word = "example"
word.count('e')

On the end you must only multiply by 5

darthir21
  • 57
  • 4
-1
def VowelOne():
    var = (Word.count('a')*5)
    return var

def VowelTwo():
    var = (Word.count('e')*4)
    return var

Word = input("Please enter a word (from the english dictionary) if you please ")
var = VowelOne()
VarTwo = VowelTwo()

print (Word)
print(VowelOne()+VowelTwo())

This I have found to be the simplest solution; I just multiplied how many instances of a letter there are by its score. Thanks to all contributions.

The White Wolf
  • 65
  • 1
  • 2
  • 8