编写在句子中定义 aeiou 的字符串,其中包含每个变量的数量

发布于 2025-01-12 20:41:19 字数 346 浏览 2 评论 0原文

作为 python 初学者,我需要帮助:

编写一个程序: 为每个元音定义一个计数器(例如 a_counter = 0 ) 提示用户输入提示:请输入您的句子: 捕获输入并将其存储在变量中 循环遍历用户输入并计算所有元音。 遍历字符串并计算所有元音后,显示以下消息:

用户输入中 a 或 A 的数量为 ## 用户输入

中 e 或 E 的数量为 ## 用户输入

中 i 或 I 的数量用户输入是 ##

用户输入中 o 或 O 的数量是 ##

用户输入中 u 或 U 的数量是 ##

y 或用户输入中的 Y 是 ##

如果我想包含输入中输出“用户输入”的句子该怎么办? –

I need help as a beginner in python:

Write a program that:
Defines a counter for each vowel (e.g a_counter = 0 )
Prompt the user for input with the prompt: Please enter your sentence:
Capture the input and store it in a variable
Loop through the user input and count all the vowels.
Once you have traversed the string and counted all the vowels, display the following messages:

The number of a's or A's in the user input is ##

The number of e's or E's in the user input is ##

The number of i's or I's in the user input is ##

The number of o's or O's in the user input is ##

The number of u's or U's in the user input is ##

The number of y's or Y's in the user input is ##

what if I want to include the sentence from the input where it outputs "user input" instead? –

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞慾 2025-01-19 20:41:19

如果您需要计算字符串中的字母数量,我建议将字符串提供给collections.Counter。当打印输出时,您还可以通过循环遍历元音来节省大量的复制和粘贴操作:

from collections import Counter

sentence = input("Please enter your sentence:  ")
c = Counter(sentence.lower())
for v in "aeiou":
    print(f"The number of {v}'s or {v.upper()}'s in '{sentence}' is {c[v]}")
Please enter your sentence:  The quick brown fox jumps over the lazy dog.
The number of a's or A's in 'The quick brown fox jumps over the lazy dog.' is 1
The number of e's or E's in 'The quick brown fox jumps over the lazy dog.' is 3
The number of i's or I's in 'The quick brown fox jumps over the lazy dog.' is 1
The number of o's or O's in 'The quick brown fox jumps over the lazy dog.' is 4
The number of u's or U's in 'The quick brown fox jumps over the lazy dog.' is 2

If you need to count letters in a string, I suggest feeding the string to collections.Counter. You can also save yourself a lot of copying and pasting by going over the vowels in a loop when it's time to print the output:

from collections import Counter

sentence = input("Please enter your sentence:  ")
c = Counter(sentence.lower())
for v in "aeiou":
    print(f"The number of {v}'s or {v.upper()}'s in '{sentence}' is {c[v]}")
Please enter your sentence:  The quick brown fox jumps over the lazy dog.
The number of a's or A's in 'The quick brown fox jumps over the lazy dog.' is 1
The number of e's or E's in 'The quick brown fox jumps over the lazy dog.' is 3
The number of i's or I's in 'The quick brown fox jumps over the lazy dog.' is 1
The number of o's or O's in 'The quick brown fox jumps over the lazy dog.' is 4
The number of u's or U's in 'The quick brown fox jumps over the lazy dog.' is 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文