繁体   English   中英

我试图从输入中打印每个verible 的数量,但是我的代码并没有写出我想要的东西,尽管它看起来是正确的。 python

[英]Im trying to print the amount of every verible from input, but my code doesn't write what I want it though it seems right. python

这是代码,尽管它应该给出不同的 output,但它一直给我同样的东西。

    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))

这是我得到的 output:

    {}

你可以试试

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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM