我如何理解如何将代码重构为函数以供将来使用 [英] How do I understand how to refactor code in to functions for future use

查看:32
本文介绍了我如何理解如何将代码重构为函数以供将来使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python速成课程文本上课.该课程原本是亲自授课的,但已转到在线课程.我对以下任务感到困惑,并想知道是否有人可以帮助我完成任务.

I am taking a class using Python Crash Course Text. The class was supposed to be in person but has moved to online. I am confused with the following assignment and would like to know if someone can help walk me through it.

重构代码是将代码的各个部分转换为可以在代码的其他部分中再次使用的函数的过程.

Refactoring your code is the process of converting sections of your code into functions that can be reused again in other parts of your code.

在之前的作业《测试成绩中的更多乐趣》中,您应该确定是否有人获得了100分.您将进入代码部分,确定是否有人获得了100分并将其转换为名为has_100的函数()(5分).考虑一下您的函数可能需要哪些参数以及函数需要返回哪种数据类型.has_100()函数不需要打印任何信息.只需查看成绩列表,然后根据列表中是否包含100即可返回True或False.

In a previous assignment, More Fun with Test Grades, you were supposed to identify if anyone scored a 100. You are going to take the section of your code that identifies if anyone scored a 100 and convert it to a function called has_100() (5 points). Think about what parameter(s) your function may need and what data type your function needs to return. The has_100() function does not need to print any information. It only needs to look at a list of grades and return True or False depending on if the list contains 100 or not.

您还需要创建一个名为print_top_grades()的函数.为了使该功能可重用,它应该具有几个参数.(1)要打印的成绩编号,以及(2)包含成绩的列表.这与has_100()函数不同,因为print_top_grades()不会返回任何内容.它仅使用for循环以降序打印列表中的前n个成绩.注意:由于列表是可变的,因此必须注意不要对列表进行永久排序.您只想打印前n个成绩.

You also need to create a function called print_top_grades(). In order to make this function reusable, it should have a couple of parameters; (1) the number of grades to print and (2) the list containing the grades. This will be different from the has_100() function because print_top_grades() won't return anything. It simply uses a for loop to print the top n grades in the list in descending order. Note: since lists are mutable, you have to be careful not to permanently sort the list. You only want to print the top n grades.

请确保考虑一个空列表.如果要打印的等级数大于列表中的等级数,则应按降序打印所有等级.例如,如果列表中有3个成绩,但有人尝试打印前5个成绩,则仅应打印3个成绩.

Be sure to account for an empty list. If the number of grades to print is larger than the number of grades in the list, all grades should be printed in descending order. For example, if there are 3 grades in the list but someone tries to print the top 5, only 3 grades should print.

#more fun with test grades assignment
# Do not modify: required for assignment
import random
last_score = random.randint(99,100)
if bool(random.getrandbits(1)):
    grades = [71, 64, 82, 72, 56, 99, 96, 99, 86, 84, 90, last_score]
else:
    grades = []
# Do not modify: required for assignment

推荐答案

赋值很简单,您无需将整个逻辑不使用函数(如现在所写)编写,而无需将代码分成函数.

The assignment is simple, instead of writing the whole logic without using functions (as it is written now) you need to separate the code into functions.

应该是这样的:

def has_100():
    """Identifies if anyone scored a 100"""
    # your code here

def print_top_grades():
    """print the top n grades in the list in descending order"""
    # your code here

def main():
    """Here you will write the execution of the program
    reusing the functions above according to the task assigned"""
    # your code here

# This is used to execute our file when you call 'python file.py' in your 
# terminal
if __name__ == '__main__':
    main()

将需要的参数添加到函数(而不是main())中,以完成每个任务的任务.

Add the parameters you need to the functions (but not to the main()) to fulfil the task of each one.

这篇关于我如何理解如何将代码重构为函数以供将来使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆