-1

Basically, I'm working on a Python fill in the letters type of game (kind of like Hangman).

The problem is I can't seem to get the program to record duplicate points. What I mean is:

The program asks the user for a word. That word, let's say....football, is converted into a masked string (ex. **)

Then it continually asks the user for letter inputs. Let's say the user enters: f o t b a l

And then it fills out the word. For each letter that is guessed correctly, the user is awarded ONE point. But the problem is that for a word like football,only 6 points are awarded because some of the letters are duplicates.

Basically, the way I've coded it, each time a correct letter is guessed, another point is added on top of the overall total points. Is there a better way of doing this that can include the duplicate letters?

4

2 回答 2

1

您也许可以count()在单词上使用来查看该字母在单词中出现的次数:

word = 'football'
# Code here to take input
# if input is in word:
    points = word.count(the_input)
    award_player(points)
于 2013-07-12T09:22:49.327 回答
1

您可以尝试list comprehension结合sum()

>>> s = "foot**ll"
>>> sum([1 for x in s if x != '*'])
6
于 2013-07-12T09:28:45.237 回答