擅长:python、mysql、java
<p>当我运行代码时,我没有得到任何输出,这是因为在“While”循环的末尾有一个无限循环,它要求用户输入另一行,从而导致无限循环。你知道吗</p>
<p>在我稍微修改你的代码之前你在做什么</p>
<pre><code>occurences = {}
line = input('Enter line: ')
while line:
m = line.split()
for i in m:
if i in occurences:
occurences[i] += 1
else:
occurences[i] = 1
break ## Once the word has been put into occurences it breaks
## so that the next loop can run
for word in sorted(occurences):
print(word, occurences[word])
</code></pre>
<p>如果您希望它一直持续到用户退出程序,则可以执行以下操作:</p>
<pre><code>while True:
occurences = {}
line = input('Enter line: ')
while line:
m = line.split()
for i in m:
if i in occurences:
occurences[i] += 1
else:
occurences[i] = 1
break
for word in sorted(occurences):
print(word, occurences[word])
</code></pre>