Question

Python: Printing Student Data Ask the user: "How many students are in the class?" Read in...

Python: Printing Student Data

  • Ask the user: "How many students are in the class?"
  • Read in the number of students
  • If there are students
    • For each student
      • Ask for the name of the student: "Enter the name of student:"
      • Read in the name of the student
      • If the student's name is greater than 24 characters
        • Do not display this student's data
        • Do not ask for their gpa
        • Print: "Name of student cannot exceed 24 characters."
      • Ask for the gpa of the student: "Enter the gpa of student:"
      • Read in the gpa of the student
      • If the student's gpa is not valid (between 0 and 4.0, inclusively)
        • Do not display this student's data
        • Do not include their gpa in the calculation
        • Print: "GPA of student must be between 0.0 and 4.0."
    • If the number of valid students is greater than zero
      • Display a chart of student data with headers and a header horizontal dividing line:
        • Number (left aligned in 10 spaces)
        • Name (left aligned in 24 spaces)
        • GPA (left aligned, spaces don't matter)
        • Horizontal divider (37 hyphens)
        • Student data vertically aligned with headers, one student per line
      • Display the average GPA of all of the valid students: "Average GPA: #.###" (there is no limit to the number of decimal places)

Assumptions

You may assume the following

  • The number of students will be an integer
  • The number of students will not exceed 10 characters
  • The name of the student will be printable characters, you do not need to check them in any way
  • The gpa of a student will be a floating point value
  • Use the float type for floating point precision

Input(Exactly)

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

Output(Exactly)
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
  • The number of students will be an integer
  • The number of students will not exceed 10 characters
  • The name of the student will be printable characters, you do not need to check them in any way
  • The gpa of a student will be a floating point value
  • Use the float type for floating point precision

Requirements

To solve this problem, you must use the following python features:

  • len() function
  • for loop with range
  • and/or
  • continue

You may not use a string formatter

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Code:

stuNum = int(input('How many students are in the class? ')) #getting number of students
stuList = [] #to store list of students who are valid
for i in range(0, stuNum): #looping to get student data
stuData = [] #to store current student data
stuName = input('Enter the name of student: ')
if len(stuName)> 24: #validating student name
print('Name of student cannot exceed 24 characters.')
continue
stuGpa = float(input('Enter the gpa of student: '))
if stuGpa < 0 or stuGpa > 4: #validating student GPA
print('GPA of student must be between 0.0 and 4.0.')
continue
stuData.append(stuName) #appending name to current student
stuData.append(stuGpa) #appending GPA to current student
stuList.append(stuData) #adding to main list

if len(stuList) > 0: #displaying output if there are valid students
print('Number' + ' ' * 3 + 'Name' + ' ' * 20 + 'GPA') #header
print('-' * 37) #37 hypens
sumGpa = 0 #to store GPA sum
for item in stuList: #displaying result of ech student
stuNumber = stuList.index(item) + 1
stuName = item[0]
stuGpa = item[1]
sumGpa += stuGpa #calculating sum
print(str(stuNumber) + ' ' * (10 - len(str(stuNumber)) - 1) + stuName + ' ' * (24 - len(stuName)) + str(stuGpa))
print('Average GPA: ' + str(sumGpa/(len(stuList)))) #displaying avg GPA

Sample Output:

Add a comment
Know the answer?
Add Answer to:
Python: Printing Student Data Ask the user: "How many students are in the class?" Read in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python : Need help with output of table Code: # Number of students print('How many students...

    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...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Please help, provide source code in c++(photos attached) develop a class called Student with the following protected...

    Please help, provide source code in c++(photos attached) develop a class called Student with the following protected data: name (string), id (string), units (int), gpa (double). Public functions include: default constructor, copy constructor, overloaded = operator, destructor, read() to read data into it, print() to print its data, get_name() which returns name and get_gpa() which returns the gpa. Derive a class called StuWorker (for student worker) from Student with the following added data: hours (int for hours assigned per week),...

  • PYTHON*************** Create a Person Class that: Accepts First Name, Last Name, Age, and Gender Has a...

    PYTHON*************** Create a Person Class that: Accepts First Name, Last Name, Age, and Gender Has a method that adds all persons Validate all input, not left empty for First Name, Last Name, Gender and numeric for age and throw an exception if it does not validate within the class. Create a Student Class that: Inherits the Person Class Accepts GPA Validate GPA (>= 0 and <= 4.0) and throw exception if it does not validate within the class. Has methods...

  • You are asked to define a user-defined data type for students, that will contain the following...

    You are asked to define a user-defined data type for students, that will contain the following information about a student: ID number, first name, last name, major, GPA. Your structure should be cal Student. Based on the defined structure, user name on the command line, show how you will use a loop to search for the given name within the array of 5 students. use an array to record information about 5 students. Then, given a Write a C program...

  • In python, Implement a function studentID() which allows the user to enter the 7-digit student ID....

    In python, Implement a function studentID() which allows the user to enter the 7-digit student ID. The program will keep prompting the user for a last name and first name. If the student does not have a student ID on record, the program will then ask for the student ID, and store that information. If the student already has a student ID, the program will display it, and ask for confirmation whether a new studentID should be assigned (and, if...

  • For C++ program Your program should first prompt the user for the number of students they...

    For C++ program Your program should first prompt the user for the number of students they wish to enter. For each student, the user will be prompted to enter the student’s name, how many tests the student has taken, and the grade for each test. Each test’s grade will be inputted as a number representing the grade for that test. Once each student’s information has been entered, the program will display the number of students. Additionally, each student will have...

  • you must implement public class Student by following the instructions as follows exactly. You may reference...

    you must implement public class Student by following the instructions as follows exactly. You may reference Chapter 7 for the similar examples to get the ideas and concepts. Each student must have the following 5 attributes (i.e., instance variables): private String   studentID ;              // student’s ID such as                         1 private String lastName   ;                         // student’s last name such as              Doe private String firstName ;             // student’s first name such as            John private double gpa...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT