def __init__(self, firstName, lastName, city, average):
"""Construct an Applicant object"""
self.firstName = firstName
self.lastName = lastName
self.city = city
self.average = average
def getFullName(self):
"returns the full name of the applicant in the format 'lastName, firstname'"
return self.firstName + "," + self.lastName
def getCity(self):
"returns the city of the Applicant"
return self.city
def getAverage(self):
"returns the Applicant average"
return self.average
def getScholarshipStatus(self):
"""calculate and return the scholarship status based on the following:
return None if average < 90
return General if average >= 90
"""
if self.average < 90:
return none
else:
return "General"
def getAcceptanceStatus(self):
"""calculate and return acceptance status based on criteria
return 'accepted' if average >= 80
return 'denied' if average < 80"""
if average >= 80:
return "accepted"
else:
return "denied"
def __str__(self):
"""returns the string interface"""
return "{0}, {1}, {2}, {3}".format(self.firstName,self.lastName,self.city,self.average)
这是我的Applicant python,需要用于我的Main_Applicant模块 这是我的main_applicant模块
from applicant import *
def printAcceptanceList(applicantList):
"""Output a list applicants that were accepted"""
def printScholarshipList(applicantList):
"""Output a list applicants that qualify for scholarship"""
for applicants in applicantList:
if applicants.getScholarshipStatus() == "General":
print applicants
def makeApplicant(infoStr):
"""returns an Applicant object created from the infoStr"""
appStrList = infoStr.split(",")
firstname = appStrList[0]
lastname = appStrList[1]
city = appStrList[2]
marksum = 0
for markstr in appStrList[3:]:
marksum = marksum + int(markstr)
average = marksum/6
# create the applicant object
newApplicant = Applicant(firstname, lastname, city, average)
return newApplicant
def getHighestApplicant(applicantList):
"""returns the Applicant with the highest average"""
highest_average = 0
for applicants in applicantList:
highest_average = max()
def buildApplicantList(infoStrList):
"""returns a list of Applicants from a file list"""
accepted_list = []
for applicants in applicantList:
if applicants.getAcceptanceStatus() == "Accepted":
applicants = makeApplicant(applicants)
accepted_list = append.applicant
def main():
#read the text file into a list (each row in the file is an item in the list)
file = open("applicants.csv","r")
fileList = file.readlines()
# assemble the collection of applicants
applicantsList = buildApplicantList(fileList)
printAcceptanceList(applicantsList)
printScholarshipList(applicantsList)
presScholarApp = getHighestApplicant(applicantsList)
print("The Winner of the President's Scholarship is " + presScholarApp.getFullName())
main()
在main_Applicant
模块中,applicants.csv是我用excel作为申请人的平均值,城市和其他信息在功能中使用的东西
任何人都知道如何使模块工作?
p.s如果我知道学生在哪里申请 - 一份关于每个城市申请学生人数的报告,我该如何编码呢?
答案 0 :(得分:0)
首先,您的代码需要实际包装在类声明中:
class Applicant:
def __init__(self, firstName, lastName, city, average):
"""Construct an Applicant object"""
self.firstName = firstName
self.lastName = lastName
self.city = city
self.average = average
def getFullName(self):
"returns the full name of the applicant in the format 'lastName, firstname'"
return self.firstName + "," + self.lastName
def getCity(self):
"returns the city of the Applicant"
return self.city
def getAverage(self):
"returns the Applicant average"
return self.average
def getScholarshipStatus(self):
"""calculate and return the scholarship status based on the following:
return None if average < 90
return General if average >= 90
"""
if self.average < 90:
return none
else:
return "General"
def getAcceptanceStatus(self):
"""calculate and return acceptance status based on criteria
return 'accepted' if average >= 80
return 'denied' if average < 80"""
if average >= 80:
return "accepted"
else:
return "denied"
def __str__(self):
"""returns the string interface"""
return "{0}, {1}, {2}, {3}".format(self.firstName,self.lastName,self.city,self.average)
否则selfs和 init 方法没有任何意义。
Python有一个内置的CSV阅读器。查看文档here,它非常易于使用且记录良好
编码你要求的代码你可以通过多种方式进行编码,例如你可以遍历所有学生并将城市列入一个列表,然后查看最常出现的内容。不确定这将如何影响效率,但它很容易理解,并且相当直接的编码。祝你好运