【问题标题】:python code nested dictionary not producing output? no errorspython代码嵌套字典不产生输出?没有错误
【发布时间】:2021-09-05 12:54:37
【问题描述】:

我正在尝试运行下面的代码。这只是我正在试验的简单列表和字典,但是当我运行它时它似乎没有产生任何输出,而且我看不出我哪里出错了。

我很欣赏代码没有多大意义。我只是在寻找诸如顶级王牌之类的任意值,只是为了看看我能不能做出点什么。

 bikes = {'mtb':{'sus':2, 'wheels':4},
   'bmx':{'sus':0, 'wheels':2},
   'road': {'sus':1,'wheels':4}}

 def totalItems(bikes,item):
    numItems =0
    for k, v in bikes.items():
        numItems= numItems +v.get(item, 0)
        return numItems

    print('number of items:')
     print('-sus  '+ str(totalItems(bikes,'sus')))
    print('-wheels  +str(totalItems(bikes,'wheels')))

【问题讨论】:

  • Python 没有在最后一行之前抱怨缩进问题?
  • 您的代码永远不会到达print 语句。您的函数在到达“return”时立即终止。在您的情况下,它在进入第一次运行 for 循环后立即返回。
  • 谢谢大家,我已经做了建议的修改并取得了一些成功,再次感谢您的时间

标签: python compiler-errors


【解决方案1】:

你的代码行没有对齐:

bikes = {'mtb':{'sus':2, 'wheels':4},
  'bmx':{'sus':0, 'wheels':2},
  'road': {'sus':1,'wheels':4}}

def totalItems(bikes,item):
   numItems =0
   for k, v in bikes.items():
       numItems= numItems +v.get(item, 0)
   return numItems  # <- HERE

print('number of items:')  # <- HERE
print('-sus ' + str(totalItems(bikes,'sus')))  # <- HERE
print('-wheels ' + str(totalItems(bikes,'wheels')))  # <- HERE

输出:

number of items:
-sus 3
-wheels 10

您可以使用f-strings 替换您最近的 2 个print 调用以获得更多可读性:

print(f"-sus {totalItems(bikes,'sus')}")
print(f"-wheels {totalItems(bikes,'wheels')}")

【讨论】:

    【解决方案2】:

    我猜在你的代码末尾你必须调用函数?...

    totalItems(bikes, item)
    

    或者你的意思是这个例子?

    bikes = {'mtb':{'sus':2, 'wheels':4},
      'bmx':{'sus':0, 'wheels':2},
      'road': {'sus':1,'wheels':4}}
    
    def totalItems(bikes,item):
       numItems =0
       for k, v in bikes.items():
           numItems= numItems +v.get(item, 0)
       return numItems
    
    print('number of items:')
    print('-sus  ' + str(totalItems(bikes,'sus')))
    print('-wheels ' + str(totalItems(bikes,'wheels')))
    

    要将打印功能移出功能?

    输出:

    number of items:
    -sus  3
    -wheels 10
    

    【讨论】:

    • 是的,我在剪切和粘贴到堆栈时遇到了一些问题,因此它使我的对齐方式失效。然而,即使正确对齐它似乎什么也没产生?谢谢
    • @C212 检查最后一个示例,它应该有效,如果有效,请接受并投票:)
    • return numItems 应该在循环之外,否则你会得到错误的结果。
    • @Corralien 啊,那是真的!谢谢你告诉我,编辑了我的
    【解决方案3】:

    代码在某些行之前有有问题的空格,最后一行缺少引号,return 在for loop 内,因此它在第一次迭代后终止,并在没有完成添加数字的情况下返回。

    这是带有适当缩进的代码,每个固定行后都有解释,

        bikes = {'mtb':{'sus':2, 'wheels':4},
            'bmx':{'sus':0, 'wheels':2},
            'road': {'sus':1,'wheels':4}}
    
        def totalItems(bikes, item): # There was a space before 'def'
            numItems =0
            for k, v in bikes.items():
                numItems = numItems + v.get(item, 0)
            return numItems # The return needs to be outside of the loop
                            # so it won't terminate after the first iteration
    
        print('number of items:') # There were 4 spaces before 'print'
        print('-sus  '+ str(totalItems(bikes,'sus'))) # There were 5 spaces before 'print'
        print('-wheels '  + str(totalItems(bikes,'wheels'))) # There were 4 spaces before 'print'
                                                            # There was only one quotation mark before the word '-wheels' so it wasnt running correcly
    

    建议: 在最后两行中,您可以使用.format() 方法将函数输出包含在字符串中。这是它的外观

        print('-sus {}'.format(str(totalItems(bikes,'sus'))))
        print('-wheels {}'.format(str(totalItems(bikes,'wheels'))))
    

    .format() 方法格式化您在字符串中的大括号 {} 内放入的输入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      相关资源
      最近更新 更多