简体   繁体   English

Python 3.X 麻烦,必须遍历一个列表,在另一个列表中调用函数,对齐宽度

[英]Python 3.X trouble having to iterate through a list, calling function within another, Justifying width

With this python assignment, I am trying to get the end result of producing a star for a movie rating.通过这个 python 任务,我试图获得为电影评级制作明星的最终结果。 I am having difficulties because even with one to one help, I cannot decipher how to return the list seen below in the instructions.我遇到了困难,因为即使有一对一的帮助,我也无法破译如何返回下面在说明中看到的列表。

instructions指示

"Write a function called count_ratings that will take a list of asterisks that represent an individuals rating for a given app and return a list of the number of 5 stars, 4 stars, 3 stars, 2 stars, and 1 star ratings. For example, if you called this function and passed it the list [" ", " *", "****", "* ","*****", "****", "**"] it would return the list [1, 1, 2, 2, 1] (where this means there are (1) 1-star rating, (1) 2-star rating, (2) 3-star rating, (2) 4-star ratings and (1) 5-star rating.)"* “编写一个名为 count_ratings 的函数,它将采用代表给定应用程序的个人评分的星号列表,并返回一个包含 5 颗星、4 颗星、3 颗星、2 颗星和 1 颗星评分的列表。例如,如果您调用此函数并将列表 [" ", " *", "****", "* ","*****", "****", "**"]传递给它将返回列表 [1, 1, 2, 2, 1] (这意味着有 (1) 1 星级,(1) 2 星级,(2) 3 星级,(2) 4-星级和 (1) 5 星级。)"*

At which I did that correctly, or at least I thought But as I went on to the further instructions.我正确地做到了这一点,或者至少我认为但是当我继续进行进一步的说明时。

1. Write a function called print_ratings that takes no parameters. 1. 编写一个不带参数的函数print_ratings。 This function will:此功能将:

◦ Use the code at the end of the section to create a ratings variable that represents the list of ratings that you are going to use to in this program. ◦ 使用本节末尾的代码创建一个 ratings 变量,该变量表示您将在此程序中使用的评级列表。

ratings = ["*****", " ", " ", " ", " ", "***", "*****", "****", "****","*",评分 = ["*****", " ", " ", " ", " ", "***", "*****", "****", "****" ,"*",

" ", " " ", " ", "* ", "***", "*****", "*****", "*****", "*****", "*****"] ", "* ", "***", "*****", "*****", "*****", "*****", "*****" ”]

• Call the count_ratings function passing it the list of ratings. • 调用count_ratings 函数,将评级列表传递给它。 • Take the list that count_ratings returns and iterate through it and print out the rating • 取出 count_ratings 返回的列表并遍历它并打印出评分

breakdown based on what the count_ratings function returns.根据 count_ratings 函数返回的内容进行细分。 Suppose count_ratings returns the following list [4, 2, 4, 2, 6], you want to print a histogram of this count.假设 count_ratings 返回以下列表 [4, 2, 4, 2, 6],您要打印此计数的直方图。 See the example output below.请参阅下面的示例输出。

  1. Call print_ratings from main .从 main 调用 print_ratings 。
  2. Run your code to verify that it works correctly.运行您的代码以验证它是否正常工作。

At which I had done correctly, but.. as I tried calling the variable the error was stated as 'name 'count_list' is not defined' This is the end result of the output我做对了,但是..当我尝试调用变量时,错误被声明为'name 'count_list' is not defined' 这是输出的最终结果

5 Stars: |# # # # # # # | 5 颗星:|# # # # # # # |

4 Stars: |# # | 4 颗星:|# # |

3 Stars: |# # # # | 3 颗星:|# # # # |

2 Stars: |# # | 2 星:|# # |

1 Stars: |# # # # | 1 颗星:|# # # # |

The list must be printed in descending order, labeled as shown above.该列表必须按降序打印,标记如上所示。

• Each ' # ' represents one rating of X number of stars. • 每个'#' 代表X 颗星的一个评级。 So in the example provided,there are six entries with 5 stars, two entries with 4-stars and etc.因此,在提供的示例中,有 6 个条目为 5 星,两个条目为 4 星等。

• Each visual representation of the count must be left justified, with a total width of 25. Place the pipe symbol to the left and right of the printed line • 计数的每个视觉表示都必须左对齐,总宽度为 25。将管道符号放置在打印线的左侧和右侧

Here is where I got to, any help is needed, because I cannot find the solution after switching out variables and parameters I also need help because justifying the code seems to boggle me.这是我到达的地方,需要任何帮助,因为在切换变量和参数后我找不到解决方案我也需要帮助,因为证明代码的合理性似乎让我感到困惑。

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

Three bugs with your code:您的代码存在三个错误:

  1. count_list is not defined in print_ratings() because you're using it within the scope of a different function, count_ratings() One approach to fixing this can be to bring the count_list into print_ratings() , and then passing it as an argument into count_ratings() . count_list未在print_ratings()定义,因为您在不同函数的范围内使用它, count_ratings()解决此问题的一种方法是将 count_list 带入print_ratings() ,然后将其作为参数传递给count_ratings() .

  2. When you use a for-each loop in python, ex.当您在 python 中使用 for-each 循环时,例如。 for i in st , i is the individual element, and st is the array. for i in sti是单个元素, st是数组。 So in count_ratings() , all your if-clauses should use i instead of st .所以在count_ratings() ,你所有的 if 子句都应该使用i而不是st

  3. process_user_word() is not defined. process_user_word()未定义。 Not sure if you didn't provide it on purpose or not.不知道你是不是故意提供的。

Here's the working code:这是工作代码:

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

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

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