欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python语言程序设计:Lab5

程序员文章站 2022-10-04 13:47:44
Programming Create a Class Student which should have the following information:Parameters passed to __init__ when an instance of the class is created: ......

programming

create a class student which should have the following information:
parameters passed to __init__ when an instance of the class is created:
self
p_surname - a string
p_given_name - a string
p_id - an integer
p_results - a list - see below for the expected format
these will then be used to set the following instance variables:
self.surname
self.given_name
self.id
self._results (underscore '_' means it is hidden from the user)
there will also be a method:
calc_marks()
this method will take as argument a module code e.g. 'nwu112' and it will calculate the final mark for
the student for that particular module according to the assumption:
20% project 1
20% project 2
10% multiple choice test
50% final exam
an instance of a student will be created like this:

student1 = student( \
'sutcliffe',\
'richard',\
2017123456,\
{ 'nwu112': { 'project1': 60, 'project2': 65, 'mcq_test': 70, 'final_exam': 80 },\
'nwu113': { 'project1': 68, 'project2': 57, 'mcq_test': 60, 'final_exam': 70 } } )

 

so, the results consist of a dictionary where the key is 'nwu112' or 'nwu113' and the value in each case
is another dictionary containing the different marks.
when you have your class written, create a couple of instances of the class; for each one, invent a
surname, given name, id and results list. etc.
finally, run calc_marks() on your student objects to find out their final mark for a module. in other
words, you will do:
student1.calc_marks( 'nwu112' )
your method will be in the student class and so will have access to student._results . so in
calc_marks() you will be able to do:
student._results[ 'nwu112' ]
to get the results for the module nwu112. for the project1 marks for nwu112 you will be able to do:
student._results[ 'nwu112' ] [ 'project1' ]
and the value of that, assuming the above data, is the integer 60. you can use these numbers to
calculate the overall result for a module.

 

"""student.py"""
"""student类"""
class student:
    def __init__(self,p_surname,p_given_name,p_id,p_results):
        self.surname=p_surname
        self.given_name=p_given_name
        self.id=p_id
        self._results=p_results

    def calc_marks(self,subject):
        marks=self._results[subject]
        result=marks['project1']*0.2+marks['project2']*0.2+marks['mcq_test']*0.1+marks['final_exam']*0.5
        print("the final mark of "+self.surname+" about subject "+subject+" is "+str(result))
        return result
"""test.py"""
from student import student

student1 = student( \
'sutcliffe',\
'richard',\
2017123456,\
{ 'nwu112': { 'project1': 60, 'project2': 65, 'mcq_test': 70, 'final_exam': 80 },\
'nwu113': { 'project1': 68, 'project2': 57, 'mcq_test': 60, 'final_exam': 70 } } )

student1.calc_marks('nwu112')
student1.calc_marks('nwu113')