我正在嘗試創建一個分析用戶輸入單詞的工具。它說明了字母的數量(完成),元音的數量(需要幫助),大寫字母的數量(已完成)以及最常見的字母(尚未擔心)。我也做了下面的代碼:Python:創建單詞分析
word = raw_input("Please enter a word:")
print word
if len(word) == 1:
print word + " has " + str(len(word)) + " letter."
else:
print word + " has " + str(len(word)) + " letters."
if sum(1 for v in word if v ==["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]) == 1:
print "It also has ", sum(1 for v in word if v == ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]), " vowel."
else:
print "It also has ", sum(1 for v in word if v == ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]), " vowels."
if sum(1 for c in word if c.isupper()) == 1:
print "It has ", sum(1 for c in word if c.isupper()), " capital letter."
else:
print "It has ", sum(1 for c in word if c.isupper()), " capital letters."
使用例如字「你好」,它返回如下:
HeLLo
HeLLo has 5 letters.
It also has 0 vowels.
It has 3 capital letters.
我很困惑,因爲它知道算元音的數量,包括它在答案中,卻沒有統計單詞中的元音。
我需要做一個小調整還是一個大的改變?
謝謝。
P.S.我如何將問題標記爲已回答?
沒有閱讀完所有的問題,你可能是指'在[「一」,......,「U」]' – 2015-02-24 16:33:06
你的意思,而不是在字v v如果v == [「a」... ... – poddpython 2015-02-24 16:36:47
要解決您的「PS」問題:只需點擊答案旁邊的複選標記符號,以最好地解答您的問題。 – 2015-02-24 16:48:24