python - 我试图从输入中打印每个verible 的数量,但是我的代码并没有写出我想要的东西,尽管它看起来是正确的。Python
问题描述
这是代码,尽管它应该给出不同的输出,但它一直给我同样的东西。
line = input('Car: ')
while line:
def word_count(str):
counts = dict()
words = str.split()
for word in words:
if word in counts:
counts[word] += 1
else:
counts[word] = 1
return counts
line = input('Car: ')
print( word_count(line))
这是我得到的输出:
{}
解决方案
你可以试试
line = 'temp'
while line:
line = input('Car: ')
def word_count(str):
counts = dict()
words = str.split()
for word in words:
if word in counts:
counts[word] += 1
else:
counts[word] = 1
return counts
if line:
print(word_count(line))
推荐阅读
- python - 从 json 中提取下一个条目的 Python 程序
- android - 将数据从一个 Android 应用程序无线发送到另一部手机
- java - 在实体中覆盖等于导致 LazyInitializationException
- pandas - 如何在不覆盖现有数据框的情况下附加数据框
- apache-kafka - Kafka 中的主题数量可以与客户端数量一样多吗?
- wordpress - 基于用户标记的谷歌标记
- node.js - 发现 1 个低严重性漏洞错误,当同时使用 React 和服务两个文件时
- android - 如何在 Kotlin 中使我的 java 代码更好?
- python - 如何按顺序绘制和连接点?
- javascript - 我想在 asp.net mvc 3.1 中显示 sql 表中的特定行(没有 ORM)