跳至主要內容

Programming Assignment 2

AI悦创原创2023年4月27日Python 一对一教学uicUIC Information SpacePython 一对一教学uicUIC Information Space大约 11 分钟103约 3165 字

Question - Warm up

Define a function front_times(str1, n) that returns n copies of the front. n is a non-negative int. The front of the string is the first 3 chars, or whatever is there if the string is less than length 3.

Example

str1 = 'Chocolate'
front_times(str1, 2) → 'ChoCho'

Expected output:
'ChoCho'

题目要求定义一个名为 front_times 的函数,该函数接受一个字符串 str1 和一个非负整数 n 作为输入参数。该函数的目标是返回字符串 str1 的前 3 个字符(或者如果字符串的长度小于 3,则返回整个字符串)重复 n 次的结果。

下面是详细注释的代码:

def front_times(str1, n):
    # 判断字符串 str1 的长度是否大于等于 3
    if len(str1) >= 3:
        # 如果字符串长度大于等于 3,则截取前 3 个字符作为 front
        front = str1[:3]
    else:
        # 如果字符串长度小于 3,则整个字符串作为 front
        front = str1
    
    # 返回 front 重复 n 次的结果
    return front * n

str1 = 'Chocolate'
print(front_times(str1, 2))  # 预期输出: 'ChoCho'

这段代码首先检查字符串 str1 的长度是否大于等于 3。如果是,则将前 3 个字符作为 front。如果长度小于 3,则将整个字符串作为 front。最后,函数返回 front 重复 n 次的结果。

Main Question

Create a Python class named teacher that has a constructor __init__. The constructor takes arguments (self, first_name, last_name, year_birth, student_rating) and initiate an instance with the following attributes:

    first_name
    last_name
    year_birth
    student_rating
    email = first_name.lower() + last_name.lower() + "@uic.edu.cn"
    courses = []
    age = self.this_year - year_birth
    friendliness = 50

this_year is a class attribute and it has the integer value 2023.

The class has the following methods with description in the comments below:

# Important notes to receive full score
# 1. Do not change the name of the class.
# 2. The constructor does not need to return anything.
# 3. All other methods returns either a string message or a numeric variable (do not print).

class teacher:
    # You code:
# Test your code
instance = teacher("Mike", "Li", 1985, 80)
print(instance.introduce())
print(instance.help_student())
print(instance.scold_student())
print(instance.friendly_score())
print(instance.courses)
print(instance.add_course("Python"))
print(instance.add_course("Communication"))
# Test your code
print(instance.age)
print(instance.email)
print(instance.friendliness)
print(instance.this_year)
# The following should run without error messages
try:
    instance = teacher("Amy", "Liang", 1975, 10)
except:
    raise("exception")

题目解析

1. 题目解释

题目要求创建一个名为 teacher 的 Python 类,该类具有一个名为 __init__ 的构造函数。构造函数接受参数 (self, first_name, last_name, year_birth, student_rating) 并使用以下属性初始化一个实例:

    first_name
    last_name
    year_birth
    student_rating
    email = first_name.lower() + last_name.lower() + "@uic.edu.cn"
    courses = []
    age = self.this_year - year_birth
    friendliness = 50

this_year 是一个类属性,其整数值为 2023。

该类具有以下方法,注释中描述了方法的功能:

要求不要改变类名,构造函数不需要返回任何内容,其他方法返回字符串消息或数值变量,而不是打印输出。

以下是题目的中文翻译:

创建一个名为 teacher 的 Python 类,该类有一个名为 __init__ 的构造函数。构造函数接受参数 (self, first_name, last_name, year_birth, student_rating) 并使用以下属性初始化一个实例:

    first_name
    last_name
    year_birth
    student_rating
    email = first_name.lower() + last_name.lower() + "@uic.edu.cn"
    courses = []
    age = self.this_year - year_birth
    friendliness = 50

this_year 是一个类属性,其整数值为 2023。

该类具有以下方法,注释中描述了方法的功能:

请注意以下几点以获得满分:

  1. 不要更改类名。
  2. 构造函数不需要返回任何内容。
  3. 所有其他方法要么返回字符串消息,要么返回数值变量(不要打印)。

抱歉,我的上一条回答没有完整地翻译题目。下面是关于类的方法的详细翻译和解释。

该类具有以下方法,注释中描述了方法的功能:

在我之前的回答中,我已经提供了详细的代码实现以及注释。同时,我也提供了测试代码来帮助验证代码是否正确实现了题目要求的功能。请参考前面的回答以获取完整的代码实现。

2. 注意点

# Important notes to receive full score
# 1. Do not change the name of the class.
# 2. The constructor does not need to return anything.
# 3. All other methods returns either a string message or a numeric variable (do not print).

这个要求的意思是:

  1. 构造函数(__init__ 方法)不需要有 return 语句,因为它的目的是初始化一个实例,而不是返回一个值。
  2. 对于其他方法,它们应当返回字符串消息或数值变量,而不是直接打印出结果。这意味着你需要使用 return 语句来返回一个值,而不是在方法内部使用 print 语句。

在我们之前提供的代码中,已经满足了这个要求。例如:

这样设计的好处是,这些方法的返回值可以被其他部分的代码(如主程序或其他函数)使用和处理。如果这些方法直接打印出结果,它们的输出将无法被其他代码方便地使用。

1
公众号:AI悦创【二维码】

AI悦创·编程一对一

AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh

C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh

方法一:QQ

方法二:微信:Jiabcdefh

你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度
来发评论吧~
通知
关于编程私教&加密文章