Writing a Program in Python Newest Version of Python please Print(\' ...\') must
ID: 3680950 • Letter: W
Question
Writing a Program in Python
Newest Version of Python please
Print(' ...') must be used
PYTHON
Write a gradebook program that lets a teacher keep track of test averages for his or her students. Your program should begin by asking the teacher for a number of students in their class as well as the total # of tests that will be given to the class. Validate this information to ensure that the numbers entered are positive.
Next, prompt the teacher to enter in scores for each student. Ensure that the values entered are positive - if they aren't you will need to re-prompt them. Hint: you may need to use nested loops here! A "while" loop can be placed inside of a "for" loop, if necessary.
Once your program has collected all test scores for a student it should display that student's average and move onto the next student. When all students have been calculated the program should compute the overall average score for the entire class.
Here's a sample running of your program:
Some hints:
Begin by constructing a "for" loop to iterate over all students in the class
Once you're inside of this "for" loop you will probably need another loop to handle inputting the scores for a particular student.
Big hint: Try to get your program to work first without any data validation. You can add this in later once you figure out the general structure of the program.
Remember the difference between "for" and "while" loops! "for" loops are used when you want to iterate over a know # of items, and "while" loops can be used to keep the user "caught" indefinitely until they fulfill a particular condition. You will probably need to use a combination of these loops to solve this problem.
Explanation / Answer
Below is Python Code:(newes version 3.4.3)
Code:
no_of_students = int(input(' How many students are in your class?'))
while(no_of_students <= 0): #to check if number of student is +ve
print('Invalid # of students. Try again.')
no_of_students = int(input('How many students are in your class?'))
no_of_tests = int(input(' How many tests in this class?'))
while(no_of_tests <= 0): #to check if number of test is +ve
print('Invalid # of tests. Try again.')
no_of_tests = int(input('How many tests in this class?'))
#creating an equivalent 2X2 matrix. In python it is implemented as row or rows.("no_of_student" rows
#each having length equal to "no_of_tests")
a = [[0 for x in range(no_of_tests+1)] for x in range(no_of_students+1)]
for i in range(1, no_of_students+1):
print(' **** Student ' + str(i) + ' ****')
sum = 0; #to calculate sum of marks of ith students in all tests
for j in range(1, no_of_tests+1):
score = int(input('Enter score for test #' + str(j) + ':' ))
while(score<=0):
print('Invalid score. Try again.')
score = int(input('Enter score for test #' + str(j) + ':' ))
a[i][j] = score
sum = sum + a[i][j]
sum_avg = sum/no_of_tests #avg marks of ith student.
print(' Average score for student #' + str(i) + ' is ' + str(sum_avg))
total = 0 #to calculate sum of marks of all students in all tests
for i in range(1, no_of_students+1):
for j in range(1, no_of_tests+1):
total = total + a[i][j]
total_avg = total/(no_of_students * no_of_tests) #average score of all students.
print(' Average score for all students is ' + str(total_avg))
stop = input(' Press key to terminate...')
sample output:
How many students are in your class?-4
Invalid # of students. Try again.
How many students are in your class?3
How many tests in this class?-8
Invalid # of tests. Try again.
How many tests in this class?4
**** Student 1 ****
Enter score for test #1:-5
Invalid score. Try again.
Enter score for test #1:22
Enter score for test #2:34
Enter score for test #3:45
Enter score for test #4:43
Average score for student #1 is 36.0
**** Student 2 ****
Enter score for test #1:23
Enter score for test #2:43
Enter score for test #3:45
Enter score for test #4:-4
Invalid score. Try again.
Enter score for test #4:45
Average score for student #2 is 39.0
**** Student 3 ****
Enter score for test #1:45
Enter score for test #2:34
Enter score for test #3:2
Enter score for test #4:23
Average score for student #3 is 26.0
Average score for all students is 33.666666666666664
Press key to terminate...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.