參考這張圖片1
嘿,這是我想用python撰寫的程式。我試過了,我成功地迭代了單詞,但現在我如何計算個人分數?
a_string = input("Enter a sentance: ").lower()
vowel_counts = {}
splits = a_string.split()
for i in splits:
words = []
words.append(i)
print(words)
uj5u.com熱心網友回復:
您可以使用 translate 標記元音以將所有元音轉換為 'a's 。然后使用 count 方法計算每個單詞中的 'a':
sentence = "computer programmers rock"
vowels = str.maketrans("aeiouAEIOU","aaaaaaaaaa")
flagged = sentence.translate(vowels) # all vowels --> 'a'
counts = [word.count('a') for word in flagged.split()] # counts per word
score = sum(1 if c<=2 else 2 for c in counts) # sum of points
print(counts,score)
# [3, 3, 1] 5
uj5u.com熱心網友回復:
這是有關如何使用 Python 計算字串中元音數量的鏈接... :) https://www.tutorialspoint.com/How-to-Count-the-Number-of-Vowels-in-a-string -使用-Python
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/417511.html
標籤:
