使用 for 循环计算用户输入中的单个字母

发布于 2025-01-09 01:32:30 字数 300 浏览 3 评论 0原文

我正在努力完成一项任务,计算用户输入中的字母并返回在输入的句子中可以找到该字母的次数。

这就是我到目前为止所得到的:

sentence = input("write an sentence here: ")

count = 0

for character in sentence:
    if e in character:
        count += 1
    print count

我收到错误。将不胜感激任何帮助。

I am struggling with a task, counting a letter from a user input and returning the number of times the letter can be found within the sentence from the input.

this is what I have so far:

sentence = input("write an sentence here: ")

count = 0

for character in sentence:
    if e in character:
        count += 1
    print count

I am getting errors. Would appreciate any help.

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

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

发布评论

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

评论(1

呆° 2025-01-16 01:32:30

如果您只想计算字符串中的所有字母(不带空格),那么这里就可以了:

import string
letters = string.ascii_lowercase
sentence = input("write an sentence here: ")
count = 0

for letter in sentence:
    if letter in letters:
        count+=1
print(count)

如果您想搜索某个字母:


sentence = input("write an sentence here: ")
count = 0
searchedletter= input("Put your searched letter: ")
for letter in sentence:
    if letter==searchedletter:
        count+=1
print(count)

If you just want to count all the letters in string, without spaces, then here you have it:

import string
letters = string.ascii_lowercase
sentence = input("write an sentence here: ")
count = 0

for letter in sentence:
    if letter in letters:
        count+=1
print(count)

If you want to search for a certain letter:


sentence = input("write an sentence here: ")
count = 0
searchedletter= input("Put your searched letter: ")
for letter in sentence:
    if letter==searchedletter:
        count+=1
print(count)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文