如何制作一个函数,调用并打印每个学生的名字和课程

2024-09-29 03:40:53 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我到目前为止的情况

from CSE_324_course import Course
from CSE_324_skeleton_student import Student

math = Course("Algebra I")
language = Course("Spanish I")
science = Course("Earth Science")
history = Course("U.S. History I")
phys_ed = Course("Physical Education I")
speaking = Course("Speech I")
art = Course("Art I")

test_student = Student("Jill", "Sample")

test_student.add_course(math)
test_student.add_course(language)
 test_student.add_course(science)
test_student.add_course(history)

test_student2 = Student("Bill", "Sample")

test_student2.add_course(math)
test_student2.add_course(phys_ed)
test_student2.add_course(science)
test_student2.add_course(history)

test_student3 = Student("Kim", "Sample")

test_student3.add_course(language)
test_student3.add_course(speaking)
test_student3.add_course(science)
test_student3.add_course(art)

student_list=[test_student,test_student2,test_student3]

for (test_student,test_student2,test_student3 : get_course)
if (test_student().equals(search))
 System.out.println(teststudnetgetCourse());


#Each iteration should:
    #get,concatenate, and print the first and last name of the student
    #print all courses for that student
    #print a blank line between students

''对于这一部分,您可能需要查看其他基本代码: -查看如何从列表中获取项目 -查看该文件中是否有可以在此文件中调用的代码(如函数) -请验证运行此文件是否可以使用该文件中的信息获得正确的输出 另外,请检查从列表f中提取项的语法

代码2页 课程导入课程

class Student:

student_id = 0

def __init__(self, first_name, last_name):
    self.first_name = first_name
    self.last_name = last_name
    self.courses = []
    self.student_id = Student.student_id
    Student.student_id += 1

def __str__(self):


    # TODO You will need to use a variable in the loop, so you must intialize it here,
    # that variable will need to be initalized to get items listed in the first def _init_ section
    # TODO add a loop that will go through the course list
        # TODO Add code here to create a string representation of a student,
        # including first and last name and all courses that student is taking
    return "complete this return statement based on your in loop variable"

def get_first_name(self):
    return self.first_name

def get_last_name(self):
    return self.last_name

def get_student_id(self):
    return self.student_id

def add_course(self, new_course):
    # TODO add code to append new_course to self.courses
    print "Course not yet added, implementation needed."

第3页 课程:

    def __init__(self, course_name):
        self.course_name = course_name

    def __str__(self):
        return self.course_name

Tags: nametestselfaddidgetdefstudent
1条回答
网友
1楼 · 发布于 2024-09-29 03:40:53

我认为你在寻求改变

for (test_student,test_student2,test_student3 : get_course)
    if (test_student().equals(search))
        System.out.println(teststudnetgetCourse());

(您不适当地缩进)到:

for student in student_list:
    print("{} {}".format(student.first_name, student.last_name))
    for course in student.courses:
        print(course) # This won't work because "2 page of code Course import Course" needs to be finished
    print("\n") # blank line between students

相关问题 更多 >