Sushmitha_python_5
docx
keyboard_arrow_up
School
Webster University *
*We aren’t endorsed by this school
Course
5110
Subject
Mathematics
Date
Apr 3, 2024
Type
docx
Pages
4
Uploaded by CommodoreBoulder13754
ASSIGNMENT-5
Name: Sushmitha Guru Student ID: 1212496
PROBLEM_1
1.
Main.py file
from Student import Student
import Utils def main
(): file = "StudentData1.txt" SD = Utils.read_data(file) menu = """ option 1: search student by id (e.g., 1-
29347) option 2: search student by last_name (e.g., 2-Baker) option 3: search student by first name(e.g.,
3-Bill) option 4: list students with hours completed > number(e.g., 4-50)
option 5: list students greater than given gpa (e.g.,5-3.2) option 0: quit """ option = -
1 while option != 0
: print
(menu) option = input
(
'please choose an option: '
)
parts = option.split(
'-'
) if option == '0'
:
break elif parts[
0
] == '1'
: try
: st = SD[
int
(parts[
1
])] print
(st)
except KeyError
: print
(
'student with id='
,
parts[
1
]
, ' does not exist'
) elif parts[
0
]== '2'
: try
: st = [x for x in SD.values() if x.last_name==parts[
1
]] print
(st) except KeyError
: print
(
'student with last name='
,
parts[
1
]
, ' does not exist'
) elif parts[
0
] == '3'
: try
: st = [x for x in SD.values() if x.first_name==parts[
1
]]
print
(st) except KeyError
: print
(
'student with first name='
,
parts[
1
]
, ' does not exist'
) elif parts[
0
]== '4'
: try
: st = [x for x in SD.values() if x.credit_hours_completed>
int
(parts[
1
])] st.sort(
key
=
lambda x:x.credit_hours_completed) print
(st) except KeyError
: print
(
'no students found
withcredit_hours_completed>'
,
parts[
1
]
, ' does not exist'
) elif
parts[
0
] == '5'
: try
: gpa = float
(parts[
1
]) st = list
(
filter
(
lambda x:x.gpa > gpa
, SD.values()))
st.sort(
key
=
lambda x:x.gpa) print
(st) except KeyError
: print
(
'---error---'
)
print
(
'done..'
) main() 2.
Utils.py file
from Student import Student def read_data
(file_name): lines = open
(file_name
, 'r'
)
SD = {} for line in lines: line = line.strip(
'
\n
'
) parts = line.split(
'
\t
'
) # open file in read mode s1 = Student(
first_name
=parts[
1
]
, last_name
=parts[
2
]
, id
=
int
(parts[
0
])
,
major
=parts[
3
]
, credit_hours_completed = int
(parts[
4
])
,
gpa = float
(parts[
5
])) SD[s1.id] = s1 return
SD 3.
Student.py file
class Student(): def init (
self
, **kwargs): self
.id = kwargs.get(
'id'
,
0
) self
.first_name =
kwargs.get(
'first_name'
,
'None'
) self
.last_name =
kwargs.get(
'last_name'
,
'None'
)
self
.major
=
kwargs.get(
'major'
,
'None'
)
self
.credit_hours_completed = kwargs.get(
'credit_hours_completed'
,
0
) self
.gpa
= kwargs.get(
'gpa'
,
0
) def str (
self
): return self
.first_name + " : " + self
.last_name + " : "
+
str
(
self
.id) + " : " + str
(
self
.credit_hours_completed) + " : " + str
(
self
.gpa) def repr (
self
): return self
.first_name + " : " + self
.last_name + " : "
+
str
(
self
.id) + " : " + str
(
self
.credit_hours_completed) + " : " + str
(
self
.gpa) 4.
Jai.py file
file= open
(
'StudentData1.txt'
,
'w'
) file.write(
'12341'
+
'
\t
'
+
'Bill'
+
'
\
t
'
+
'Baker'
+
'
\t
'
+
'CS'
+
'
\t
'
+
'45'
+
'
\t
'
+
'3.
5'
+
'
\n
'
)
file.write(
'12342'
+
'
\t
'
+
'Sally'
+
'
\t
'
+
'Simpson'
+
'
\t
'
+
'Math'
+
'
\t
'
+
'52'
+
'
\t
'
+
'3.8'
+
'
\n
'
) file.write(
'12343'
+
'
\t
'
+
'Jim'
+
'
\t
'
+
'Jacobs'
+
'
\t
'
+
'EE'
+
'
\
t
'
+
'37'
+
'
\t
'
+
'2.
8'
+
'
\n
'
) file.write(
'12344'
+
'
\t
'
+
'Cindy'
+
'
\t
'
+
'Corman'
+
'
\t
'
+
'CS'
+
'
\t
'
+
'54'
+
'
\t
'
+
'
3.1'
+
'
\n
'
) file.write(
'12345'
+
'
\t
'
+
'Mark'
+
'
\t
'
+
'Mathews'
+
'
\t
'
+
'Math'
+
'
\
t
'
+
'73'
+
'
\t
' +
'3.3'
+
'
\n
'
) file.write(
'12346'
+
'
\t
'
+
'Jill'
+
'
\t
'
+
'Johnson'
+
'
\t
'
+
'EE'
+
'
\t
'
+
'81'
+
'
\t
'
+
'
3.6'
+
'
\n
'
) file.close() 5.
StudentData1.txt file
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
OUTPUT :-