而不是显示有多少元音出现在最后计算它们。我该如何解决这个问题?
def main():
sentence=input("Please type a sentence")
countA=0
for i in sentence:
if i =="a":
countA+=1
print("The letter a appears:", countA,"times")
countE=0
for i in sentence:
if i == "e":
countE+=1
print("The letter e appears:", countE,"times")
countI=0
for i in sentence:
if i == "i":
countI+=1
print("The letter i appears:", countI,"times")
countO=0
for i in sentence:
if i == "o":
countO+=1
print("The letter o appears:", countO,"times")
countU=0
for i in sentence:
if i == "u":
countU+=1
print("The letter u appears:", countU,"times")
countY=0
for i in sentence:
if i == "y":
countY+=1
print("The letter y appears:", countY,"times")
main()
答案 0 :(得分:0)
Sentence.count()
会更容易,但我为你解决了这个问题。从for循环中取出打印命令。
sentence=input("Please type a sentence")
countE=0
for i in sentence:
if i == "e" or i == "E":
countE+=1
print("The letter e appears:", countE,"times")
countI=0
for i in sentence:
if i == "i" or i == "I":
countI+=1
print("The letter i appears:", countI,"times")
countO=0
for i in sentence:
if i == "o" or i == "O":
countO+=1
print("The letter o appears:", countO,"times")
countU=0
for i in sentence:
if i == "u" or i == "U":
countU+=1
print("The letter u appears:", countU,"times")
countY=0
#Or use sentence.count(letter)
#for i in sentence:
# if i == "y" or i == "Y":
# countY+=1
sentence.lower()
print("The letter y appears:", sentence.count("y"),"times")