Question
Python : Need help with output of table Code: # Number of students print('How many students are in the class?') student_Number = int(input()) # Store
Python : Need help with output of table
Code:
# Number of students print('How many students are in the class?') student_Number = int(input())
# Store list of students who are valid student_List = []
for i in range(0, student_Number): # To store current student data student_Data = []
print('Enter the name of student:') # Name of Students student_Name = input() if len(student_Name)> 24: print('Name of student cannot exceed 24 characters.') continue print('Enter the gpa of student:') # GPA of Student student_Gpa = float(input()) if student_Gpa < 0 or student_Gpa > 4: print('GPA of student must be between 0.0 and 4.0.') continue # Adding name to current student student_Data.append(student_Name)
# Adding gpa to current student student_Data.append(student_Gpa)
# Adding students to main list student_List.append(student_Data)
# Displaying Output for valid students if len(student_List) > 0: # Header print('Number Name GPA') print('-' * 37) # To store GPA sum sum_Gpa = 0 # Displaying result for each student for student in student_List: student_Number = student_List.index(student) + 1 student_Name = student[0] student_Gpa = student[1] #Calculating sum GPA sum_Gpa += student_Gpa
print(str(student_Number) + ' ' * (10 - len(str(student_Number)) - 1) + student_Name + ' ' * (24 - len(student_Name)) + str(student_Gpa))
# Displaying Average GPA print("") print('Average GPA: ' + str(sum_Gpa/(len(student_List))))
Output should look like this: (skips students with long names and GPA not in the range between 0.0 and 4.0.Table assigns students numbers with valid names and GPA)
How many students are in the class? 5 Enter the name of student: Stan Marsh Enter the gpa of student: 3.6 Enter the name of student: Kenny McCormick Enter the gpa of student: 2.3 Enter the name of student: Someone with a really long name more than 20 characters Name of student cannot exceed 24 characters. Enter the name of student: Eric Cartman Enter the gpa of student: -1.1 GPA of student must be between 0.0 and 4.0. Enter the name of student: Kyle Broflovski Enter the gpa of student: 4.0 Number Name GPA --------------------------------------------- 1 Stan Marsh 3.6 2 Kenny McCormick 2.3 5 Kyle Broflovski 4.0
Average GPA: 3.3000000000000003
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