Python 计算单词质量的算法
我必须写一个函数,只接受大写的单词,并根据元音计算它的质量,问题是这样的;“元音是根据它们在字母表中的位置排列的,所以A=1,E=5,I=15等等,元音存在于我的单词中,如果它存在,那么我们就做它的范围*它在单词中的位置来找到质量。 我的尝试:Python 计算单词质量的算法,python,python-3.x,Python,Python 3.x,我必须写一个函数,只接受大写的单词,并根据元音计算它的质量,问题是这样的;“元音是根据它们在字母表中的位置排列的,所以A=1,E=5,I=15等等,元音存在于我的单词中,如果它存在,那么我们就做它的范围*它在单词中的位置来找到质量。 我的尝试: ch=input() def poids(word): poids=0 alpha="ABCDEFGHIJKLMNOPQRSTUVXYZ" voy=["A","E"
ch=input()
def poids(word):
poids=0
alpha="ABCDEFGHIJKLMNOPQRSTUVXYZ"
voy=["A","E","I","O","U","Y"]
for i in range(len(alpha)):
p=0
p1=0
for j in voy:
if alpha[i]==j:
p=alpha.find(j)+1
print("p=",p)
for k in range(len(ch)):
for m in voy:
if ch[k]==m:
p1=ch.find(m)
poids=p*p1
return poids
print(poids(ch))
我会这样做的,请试一试
import string
alphas = string.ascii_uppercase
lookup = {char: idx for idx, char in enumerate(alphas, 1)} # a dictionary
print(alphas) # can comment out later
print(lookup) # can comment out
def poids(word):
weight = 0
vowels = 'AEIOU'
for char in word:
if char in vowels:
weight += lookup[char]
else:
continue
return weight
if __name__ == '__main__':
word = input('type a word: ')
print(poids(word))
你能给出一些示例输入和预期输出吗?例如,像“密西西比州”(作为输入词)这样的单词-预期结果是什么?问题中的范围是什么意思?我从PO中的理解是:
字符范围
==分数。a=1,E=5。。。