【问题标题】:Python 3.X trouble having to iterate through a list, calling function within another, Justifying widthPython 3.X 麻烦不得不遍历一个列表,在另一个中调用函数,对齐宽度
【发布时间】:2015-10-30 03:48:31
【问题描述】:

通过这个 python 任务,我试图获得为电影评级生成明星的最终结果。我遇到了困难,因为即使有一对一的帮助,我也无法破译如何返回下面在说明中看到的列表。

说明

"编写一个名为 count_ratings 的函数,它将获取一个星号列表,这些星号表示 个人对给定应用程序的评分并返回 5 星、4 星的数量列表, 3 星、2 星和 1 星评级。 例如,如果您调用此函数并将列表传递给它 ["", "*", "****", "*","*****" , "****", "**"] 它将返回列表 [1, 1, 2, 2, 1] (这意味着有 (1) 1 星评级,(1) 2 星评级, (2) 3 星评级, (2) 4 星评级和 (1) 5 星评级。)"*

我正确地做到了,或者至少我认为但是当我继续进行进一步的说明时。

1.编写一个不带参数的名为 print_ratings 的函数。该函数将:

◦ 使用本节末尾的代码创建一个评级变量, 表示您将在此程序中使用的评级列表。

评分 = [“*****”、“”、“”、“”、“”、“***”、“*** **”、“****”、“****”、“*”、

”、“”、“*”、“***”、“*****”、“*****”、“ *****", "*****", "*****"]

• 调用count_ratings 函数,将评级列表传递给它。 • 获取count_ratings 返回的列表并遍历它并打印出评分

根据 count_ratings 函数返回的内容进行细分。认为 count_ratings 返回如下列表[4, 2, 4, 2, 6],要打印直方图 这个计数。请参阅下面的示例输出。

  1. 从 main 调用 print_ratings。
  2. 运行您的代码以验证它是否正常工作。

我做得正确,但是..当我尝试调用变量时,错误被声明为 'name 'count_list' is not defined' 这是最终结果输出

5 星: |#######|

4 星: |# # |

3 星: |# # # # |

2 星: |# # |

1 星: |# # # # |

列表必须按降序打印,如上所示。

• 每个“#”代表一个 X 颗星的评级。因此,在提供的示例中,有 6 个 5 星条目、2 个 4 星条目等。

• 计数的每个视觉表示必须左对齐,总宽度为 25。 将管道符号放置在打印线的左右两侧

这就是我要去的地方,需要任何帮助,因为在切换变量和参数后我找不到解决方案我也需要帮助,因为证明代码的合理性似乎让我感到困惑。

def count_ratings(st):
   count_list = [0,0,0,0,0]
   for i in st:
   if st == "*":
         count_list[0] = count_list[0] + 1

   elif st == "**":
         count_list[1] = count_list[1] + 1
   elif st == "***":
         count_list[2] = count_list[2] + 1

   elif st == "****":
         count_list[3] = count_list[3] + 1  
   else:
        if st == "*****":
         count_list[4] = count_list[4] + 1
   return count_list
def print_ratings():
   ratings = ["*****", "***", "**", "*****", "****", "**", "*", "***","**","*","**","*","*****","****","*****"]
   count_ratings(ratings)
   for i in count_list:
     print (count_list)
def main(): # defining the main
   process_user_word() # calling the process user word
   print_ratings()
main()

【问题讨论】:

    标签: python python-3.x iteration nested-lists calling-convention


    【解决方案1】:

    您的代码存在三个错误:

    1. count_list 未在 print_ratings() 中定义,因为您在不同的功能范围内使用它,count_ratings() 解决此问题的一种方法是将 count_list 带入 print_ratings(),并且然后将其作为参数传递给count_ratings()

    2. 当您在 python 中使用 for-each 循环时,例如。 for i in sti 是单个元素,st 是数组。所以在count_ratings(),你所有的if子句都应该使用i而不是st

    3. process_user_word() 未定义。不知道你是不是故意提供的。

    这是工作代码:

    def count_ratings(st, count_list):
       for i in st:
          if i == "*":
             count_list[0] = count_list[0] + 1
          elif i == "**":
             count_list[1] = count_list[1] + 1
          elif i == "***":
             count_list[2] = count_list[2] + 1
          elif i == "****":
             count_list[3] = count_list[3] + 1  
          else:
             if i == "*****":
                count_list[4] = count_list[4] + 1
    
       return count_list
    
    def print_ratings():
       ratings = ["*****", "***", "**", "*****", "****", "**", "*", "***","**","*","**","*","*****","****","*****"]
       count_list = [0,0,0,0,0]
       count_list = count_ratings(ratings, count_list)
    
       for i in count_list:
         print(count_list)
    
    def main(): # defining the main
       print_ratings()
    
    main()
    

    【讨论】:

      猜你喜欢
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      相关资源
      最近更新 更多