Python语言程序设计:Lab5

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

 

    原文作者:槐城一只猫
    原文地址: https://www.cnblogs.com/zijeak/p/11608023.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞