Python 3 - 编写一个程序,让用户输入一个字符串,并显示字符串中最常出现的字符。
这是我到目前为止的尝试,我知道它需要做很多工作:
def main():
count = 0
my_string = input('Enter a sentence: ')
for ch in my_string:
if ch == 'A' or ch == 'a':
count+=1
print('The most popular character appears ', count, 'times.')
main()
答案 0 :(得分:-1)
请找到以下代码:
import collections
def main():
d = collections.defaultdict(int)
my_string = input('Enter a sentence: ')
for c in my_string.strip().lower():
d[c] += 1
val=dict(d).values()
print('The most popular character appears ', sorted(val,reverse=True)[0], 'times.')
main()