我有一个我需要做的简单任务;让我说我有一个词,任何一个词。在那个词中,我需要找到有多少个字母的实例。我发现的那封信有一个值,让我们说5.然后我需要将那个字母的实例数乘以5,以积累总分。听起来很简单,但我缺乏知识才能实现它。我已经开始编写测试代码了,但即使这样也行不通。任何帮助将不胜感激。
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)
我知道没有任何变量有价值的迹象,但我不知道该怎么做。
答案 0 :(得分:1)
那么,有效地,拼字游戏?
一个很好的简单方法是创建一个分数字典,然后简单地浏览单词,在字典中查找值。您可以添加验证并简化它,但一般逻辑可能是(假设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))
答案 1 :(得分:0)
我认为你有一个小错误。删除函数的第二行?
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")
答案 2 :(得分:0)
str.count(sub [,start [,end]])
示例:
word = "example"
word.count('e')
最后你必须乘以5
答案 3 :(得分:-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())
我发现这是最简单的解决方案;我只是通过它的分数乘以一个字母的实例数。感谢所有的贡献。