擅长:python、mysql、java
<p>如果你正在寻找一个易于阅读的解决方案,接近你想要实现的,那就可以了</p>
<pre><code>def main():
x = {}
for word in sentence:
if word != " ":
x[word] = sentence.count(word)
maximum_occurrences = max(x.values())
for letter,occurrences in x.items():
if occurrences == maximum_occurrences:
print("Max value found, the letter "+ letter + " was found " + str(occurrences)+ " times.")
sentence=input("Enter a sentence: ")
main()
>>>> Enter a sentence: This is simply a test
>>>> Max value found, the letter s was found 4 times.
</code></pre>
<p>基本上,它返回任何出现次数最多的字母。请注意,这会处理多个字母出现次数相同的情况。你知道吗</p>
<p><strong>编辑</p>
<p>另外请注意,我添加了<code>if word != " ":</code>,因为您的初始代码将空格视为可能不是您想要的单词。你知道吗</p>