# Final Python Exam ### Name: Tyler Eck ### Student ID: 410821337 ## Question 1 ### Main Code: ```python #Tyler Eck #4120821337 #Python Exam Question 1 import csv import sys import codecs dep = { '中文系':'01', '英美系':'02', '歷史系':'03', '應數系':'11', '化學系':'12', '生科系':'13', '資工系':'21', '材料系':'22', '電機系':'23', '光電系':'25', } dep_count = { '中文系': 1, '英美系': 1, '歷史系': 1, '應數系': 1, '化學系': 1, '生科系':1, '資工系':1, '材料系':1, '電機系':1, '光電系':1, } new_table = [] header = ['Department','Name','StudentID'] #used to get student number to add to the end def get_num(num): if len(str(num)) == 1: n = '00'+str(num) return n elif len(str(num)) == 2: n = '0'+str(num) return n elif len(str(num)) == 3: return str(num) #used to calculate student_id def calculate_student_id(depart): student_id = '4111' + dep[depart] + get_num(dep_count[depart]) dep_count[depart]+=1 return student_id def does_file_exist(file): try: file = codecs.open(file,'r','utf-8') return 1 except FileNotFoundError: raise FileNotFoundError def main(): c = 0 if (does_file_exist("students.csv")): file = codecs.open("students.csv",'r','utf-8') reader = csv.DictReader(file) for row in reader: rows = [] depart= str(row['Department']) name = str(row['Name']) student_id = calculate_student_id(depart) rows.append(depart) rows.append(name) rows.append(student_id) new_table.append(rows) else: print("File does not exist") sys.exit(1) #open the write files file = codecs.open('students_withID.csv','w','utf-8') writer = csv.DictWriter(file,fieldnames=header) writer.writeheader() for row in new_table: writer.writerow({'Department': row[0], 'Name': row[1] ,'StudentID':row[2]}) if __name__ == "__main__": main() ``` ### Test Code: ```python #Tyler Eck #4120821337 #Python Exam Question 1 Test Code from q1 import get_num, does_file_exist import pytest def main(): test_get_num() test_does_file_exist() def test_get_num(): assert get_num(1) == '001' assert get_num(23) == '023' assert get_num(165) == '165' def test_does_file_exist(): #to check if file is not found with pytest.raises(FileNotFoundError): does_file_exist("invalid_file1.csv") #to check if file is not found with pytest.raises(FileNotFoundError): does_file_exist("invalid_file2.csv") #nothing is wrong assert does_file_exist("students.csv") == 1 if __name__ == "__main__": main() ``` ### Screenshot: ![](https://i.imgur.com/ipsaFJi.png) ### Created File: students_withID.csv ```csv Department,Name,StudentID 電機系,Eva Pedrazzini,411123001 光電系,Dott. Guarino Palumbo,411125001 歷史系,徐慧君,411103001 中文系,陳哲瑋,411101001 電機系,Alice Boone,411123002 電機系,Pamela Myers,411123003 英美系,瞿惠如,411102001 英美系,Alberico Franzese,411102002 中文系,陳俊賢,411101002 資工系,藤原 涼平,411121001 化學系,太田 晃,411112001 歷史系,佐藤 桃子,411103002 英美系,吳思穎,411102003 電機系,小川 康弘,411123004 資工系,Kimberly Miller,411121002 光電系,Azeglio Renault-Pavone,411125002 化學系,山本 智也,411112002 應數系,Osvaldo Fo,411111001 化學系,Mr. Ronald Ramirez,411112003 材料系,溫龍,411122001 中文系,Robert Johnson,411101003 材料系,森 陽一,411122002 光電系,Giampiero Tomaselli-Bonolis,411125003 光電系,高橋 あすか,411125004 化學系,遠藤 康弘,411112004 電機系,Giacinto Sabbatini,411123005 應數系,Deborah Pierce DVM,411111002 資工系,Edwin Phillips,411121003 電機系,石川 学,411123006 歷史系,Silvia Aporti,411103003 中文系,劉佳慧,411101004 英美系,Shawn Miller,411102004 中文系,周美玲,411101005 資工系,王淑惠,411121004 歷史系,張慧君,411103004 化學系,村上 幹,411112005 英美系,佐藤 結衣,411102005 歷史系,孫詩涵,411103005 英美系,Sig.ra Sonia Missoni,411102006 英美系,李宜君,411102007 英美系,Travis Ward,411102008 電機系,Christopher Stone,411123007 生科系,Emily Stephens,411113001 材料系,曾哲瑋,411122003 化學系,Lisa Miniati,411112006 光電系,田中 あすか,411125005 應數系,Adele Lercari,411111003 應數系,橋本 直子,411111004 電機系,松本 結衣,411123008 化學系,Gemma Blasi,411112007 應數系,鄭雅慧,411111005 歷史系,山本 零,411103006 英美系,Annunziata Pace,411102009 生科系,Ronnie Calhoun,411113002 歷史系,Mr. David Stephens MD,411103007 電機系,Andrea Bush,411123009 化學系,Benedetto Bernetti,411112008 應數系,Alessia Nitti,411111006 歷史系,Gelsomina Valier,411103008 資工系,Raffaele Porcellato,411121005 材料系,Sig. Fabrizio Rosiello,411122004 光電系,Zachary Bryant,411125006 生科系,Sarah Mcdonald,411113003 化學系,Charles Obrien,411112009 英美系,宋美琪,411102010 材料系,Tricia Martinez,411122005 應數系,Jessica Gilbert,411111007 生科系,Veronica Babati,411113004 生科系,佐藤 知実,411113005 資工系,張欣怡,411121006 生科系,楊淑芬,411113006 應數系,Julie Houston,411111008 生科系,Gaetano Ovadia,411113007 光電系,Steven Evans DDS,411125007 材料系,張雅涵,411122006 中文系,鄭淑娟,411101006 中文系,Daria Pezzali,411101007 化學系,Ferdinando Iacobucci,411112010 光電系,福田 太一,411125008 英美系,Kevin Brown,411102011 化學系,毛雅萍,411112011 生科系,顧欣怡,411113008 電機系,石川 洋介,411123010 生科系,Stacy Coleman,411113009 應數系,孫雅筑,411111009 生科系,Cory Farrell,411113010 資工系,周俊賢,411121007 材料系,Matteo Bonomo-Mennea,411122007 光電系,Linda Marshall,411125009 電機系,Milo Bellò,411123011 歷史系,James Pham,411103009 生科系,Fred Williams,411113011 應數系,Paul Williamson,411111010 電機系,吳志偉,411123012 資工系,Viridiana Guarato,411121008 電機系,Giovanni Pigafetta,411123013 生科系,Danielle Chan,411113012 光電系,Ann Mayo,411125010 歷史系,Gianfrancesco Faugno,411103010 材料系,楊柏翰,411122008 ``` ## Question 2 ### Code: ``` python #Tyler Eck #4120821337 #Python Exam Question 2 import csv import sys import codecs class Student(): school = "Dong Hwa University" def __init__(self, name, sID, dep): self.name = name self.sID = sID self.dep = dep def get_name(self): return self.name def get_ID(self): return self.sID def get_school(self): return self.school def get_dep(self): return self.dep def main(): #students objects list myStudents = [] file = codecs.open("students_withID.csv",'r','utf-8') reader = csv.DictReader(file) c = 0 for row in reader: depart= str(row['Department']) name = str(row['Name']) studentid = str(row['StudentID']) myStudents.append(Student(name,studentid,depart)) c+=1 if c == 10: break for i in myStudents: print("Student Name: "+i.get_name()) print("Student ID: "+i.get_ID()) print("Student School: "+i.get_school()) print("Student Department: "+i.get_dep()) print("") if __name__ == "__main__": main() ``` ### Screenshot: ![](https://i.imgur.com/yDd94wH.png) ## Question 3 ### Code: ```python #Tyler Eck #4120821337 #Python Exam Question 3 import sys import re sID = input("Enter in Student ID: ") try: with open("information.txt","r") as file: check = 0 for line in file: match = re.search(sID,line) if match: tel = line.split(" ") t = tel[3] t = t.replace("-","") #removes the dashes t = t.replace("\n","") #removes the dashes print(t) check = 1 if not check: print("Did not find") except FileNotFoundError: print("File not found") sys.exit(1) ``` ### Screenshot: ![](https://i.imgur.com/lbMYrrt.png)