Question
Python: How do I have the same input and output for printing student data Exact Input: 5 Stan Marsh 3.6 Kenny McCormick 2.3 Someone with
Python: How do I have the same input and output for printing student data
Exact Input:
5 Stan Marsh 3.6 Kenny McCormick 2.3 Someone with a really long name more than 20 characters Eric Cartman -1.1 Kyle Broflovski 4.0
Exact Output:
How many students are in the class? Enter the name of student: Enter the gpa of student: Enter the name of student: Enter the gpa of student: Enter the name of student: Name of student cannot exceed 24 characters. Enter the name of student: Enter the gpa of student: GPA of student must be between 0.0 and 4.0. Enter the name of student: Enter the gpa of student: Number Name GPA ------------------------------------- 1 Stan Marsh 3.6 2 Kenny McCormick 2.3 5 Kyle Broflovski 4.0 Average GPA: 3.3000000000000003
My Code:
# Number of Students number_of_students=int(input('How many students are in the class?'))
# To store list of students who are valid student_list=[]
for i in range(0,number_of_students):
# To store current student data student_data=[]
name_of_student=input('Enter the name of student:')
if len(name_of_student) >24: print('Name of student cannot exceed 24 characters') continue
gpa_of_student=float(input('Enter the gpa of student:'))
if gpa_of_student <0 or gpa_of_student>4: print('GPA of student must be between 0.0 and 4.0.') continue
# add name to current student student_data.append(name_of_student)
#add GPA TO current student student_data.append(gpa_of_student)
#adding to main list student_list.append(student_data)
# Output of valid students if len(student_list)>0: #Header of table print('Number' + ' ' * 3 + 'Name' + ' ' * 20 + 'GPA') print("-"*37)
#store sum of students' gpa sum_gpa =0 for j in student_list: number_of_students=student_list.index(j)+1
name_of_student=j[0]
gpa_of_student=j[1] # Calculate total sum of students' gpa sum_gpa += gpa_of_student
print(str(number_of_students) + ' ' * (10 - len(str(number_of_students)) - 1) + name_of_student + ' ' * (24 - len(name_of_student)) + str(gpa_of_student))
# Display average GPA of students' print('Average GPA: ' + str(sum_gpa/(len(student_list)))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started