簡體   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