简体   繁体   English

我的用于计算用户输入语句中的元音数量的程序不会对同一元音进行两次计数。 我该如何解决?

[英]My program for counting how many vowels in a user input sentence doesn't count the same vowel twice. How do i fix this?

print ("Sentence analysis")

Sentence = (input("Please enter a sentence"))


def WordCount(Sentence):
    words = (Sentence.count(' ')+1)
    print ("There are", words ,"words in this sentence")
WordCount(Sentence)

The code above is fine and used to count how many words in the input sentence. 上面的代码很好,可以用来计算输入句子中有多少个单词。

vowels = ['a','e','i','o','u']
count=0
for v in vowels:
    if v in Sentence:
        count+=1


print (count)

When running, say if I input aaeiou would only count 5 vowels whereas there are 6. How do I fix this? 跑步时,说如果我输入aaeiou只会数5个元音,而有6个元音。我该如何解决?

Use .count() : 使用.count()

count = 0
for v in vowels:
    count += Sentence.count(v)

Or better: 或更好:

count = sum(Sentence.count(v) for v in vowels)

That is because you are doing your check in reverse. 那是因为您正在反向进行检查。 You want to go over your sentence and check each letter against vowels: 您想遍历句子并针对元音检查每个字母:

vowels = ['a','e','i','o','u']
count=0
for s in Sentence:
    if s in vowels:
        count+=1


print (count)

For a nicer approach, however, check out @zondo's answer. 对于更好的方法,请查看@zondo的答案。

The problem you are having is that when you loop through v in vowels and check if v is in Sentence it only checks if it v is present in Sentance not how many times. 您遇到的问题是,当您在vowels循环遍历v并检查v是否在Sentence它仅检查vSentance是否存在了多少次。 If you flip it around so it checks through Sentence first and check each letter to see if it is in vowels it will check all of the letters in Sentence . 如果将其翻转,则它将首先检查Sentence并检查每个字母是否在vowels ,它将检查Sentence所有字母。

print ("Sentence analysis")

Sentence = (input("Please enter a sentence"))

vowels = ['a','e','i','o','u']
count=0
for letter in Sentence:
    if letter in vowels:
        count+=1

print (count)

For the first code can be simplified the WordCount() function as: 对于第一个代码, WordCount()函数可以简化为:

print(len(Sentence.split()))

or: 要么:

import re
print(len(re.findall(r'\w+', Sentence)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将计算结果存储在 Python 中,这样我的程序就不会对同一事物进行两次计算? - How do I store results of a computation in Python so my program doesn't compute the same thing twice? 我试图让我的程序创建一个文件来保存输入,但我不希望它出现两次。 (Python) - I'm trying to get my program to create a file which will save an input, but I don't want it to appear twice. (Python) 我的程序没有计算我想要多少欧元的门票 - My program doesn't count of how many tickets you have with some euros as I want 计算每个元音出现的次数 - Counting how many times each vowel appears 当我的程序没有告诉我任何错误时如何修复我的程序 - How do I fix my program when it doesn't tell me any errors 我如何继续循环我的程序,以便用户可以输入尽可能多的内容? - How do i keep looping the same my program so the user can input as much as they want? 当用户输入相同的输入两次(预期)时,如何将输入组合到 output? - When user inputs same input twice (expected), how do I combine the input to output? 我的代码不起作用,我该如何解决? - My Code just doesn't work, how do I fix it? 如果是元音则如何删除第一个字母,如果没有元音则如何不返回元音 - how to remove first letter if it's vowel and return no vowels if there is no vowel 如何将元音的大小写保持在同一位置? - How do I keep the capitalization of a vowel in same position?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM