Get average grade for 10 students - python - python

I have a program that asks a user to enter a Student NETID and then what grades they got on 5 assignments plus the grade they got on the mid-term and final. It then adds them and divides to get that students average grade which displays in table format.
What I need to do, is loop through that whole process 9 more times. So essentially, Ill be asking 9 more students the same input, which then Ill need to display in the table format.
My question is how would I loop through the process that I have right now 'x' amount of times, then display the average of all students.
This is my code right now:
# x holds the list of grades
x = []
# count of assignments
assignments = 5
# Ask for a student ID from user
NETID = int(input('Enter your 4 digit student NET ID: '))
# fill list with grades from console input
x = [int(input('Please enter the grade you got on assignment {}: '.format(i+1))) for i in range(assignments)]
midTermGrade = int(input('Please enter the grade you got on you Mid-Term: '))
finalGrade = int(input('Please enter the grade you got on you Final: '))
# count average,
average_assignment_grade = (sum(x) + midTermGrade + finalGrade) / 7
print()
print('NET ID \t Average Final Grade')
print('---------------------------------')
for number in range(1):
print(NETID, '\t\t', format(average_assignment_grade, '.1f'),'%')
main()
And this is how it looks on console:

You really did the hardest part. I don't see why you couldn't so the loop of the average. Anyway:
student_count = 5;
A = [student_count]
for id_student in range(student_count):
print("STUDENT #", id_student+1)
# x holds the list of grades
x = []
# count of assignments
assignments = 5
# Ask for a student ID from user
NETID = int(input('Enter your 4 digit student NET ID: '))
# fill list with grades from console input
x = [int(input('Please enter the grade you got on assignment {}: '.format(i+1))) for i in range(assignments)]
midTermGrade = int(input('Please enter the grade you got on you Mid-Term: '))
finalGrade = int(input('Please enter the grade you got on you Final: '))
# count average,
average_assignment_grade = (sum(x) + midTermGrade + finalGrade) / 7
print()
print('NET ID | Average Final Grade')
print('---------------------------------')
for number in range(1):
print(NETID, " | ", format(average_assignment_grade, '.1f'),'%')
A.append(average_assignment_grade);
grades_sum = sum(A)
grades_average = grades_sum / 5;
print("SUM OF ALL STUDENTS = " + grades_sum)
print("AVERAGE OF ALL STUDENTS = " + grades_average)
Update: As suggested above, you should make a function for a single student and loop through that function in another, since SO is not a coding service I won't do that for you, but I think you got the idea.

Related

How do I debug this code? I am doing an assessment and cannot figure anything out

print("please enter your 5 marks below")
#read 5 inputs
mark1 = int(input("enter mark 1: "))
mark2 = int(input("enter mark 2: "))
mark3 = int(input("enter mark 3: "))
mark4 = int(input("enter mark 4: "))
mark5 = int(input("enter mark 5: "))
#create array/list with five marks
marksList = [mark1, mark2, mark3, mark4, mark5]
#print the array/list
print(marksList)
#calculate the sum and average
sumOfMarks = sum(marksList)
averageOfMarks = sum(marksList)/5
#display results
print("The sum of your marks is: "+str(sumOfMarks))
print("The average of your marks is: "+str(averageOfMarks))
Couldn't really come up with anything
The assessment has the following guidelines.
Ask the user to input the marks for the five subjects in a list/array.
The program must ensure that the marks are between 0 and 100
Display the list/array of marks entered.
Find the sum of all the marks in the list (all five subjects) and display the output as:
The sum of your marks is: [sum]
Find the average of all the marks in the list (all five subjects) and display the output as:
The average of your marks is: [average mark]
If when you said "debug", you are referring to inspect the values and validate it, then you can write some unit tests
another way is to install ipdb and set a ipdb.set_trace()
How about this:
N = 5
print(f"please enter your {N} marks below")
# read inputs
def checked_input(tip: str) -> int:
try:
num = int(input(tip))
assert 0 <= num <= 100
except (ValueError, TypeError, AssertionError):
print('mark must between 0 and 100!')
return checked_input(tip)
else:
return num
marks = [checked_input(f'enter mark {i}: ') for i in range(1, N+1)]
# print the array/list
print(marks)
# calculate the sum and average
total = sum(marks)
average = total / N
# display results
print(f"The sum of your marks is: {total}")
print(f"The average of your marks is: {average}")

I'm trying to determine the min value in my array, but I keep getting traceback

Can someone help me identify where I went wrong with this code? I'm trying to determine the min value in my array. I collect student name, number and mark and am looking for the lowest mark.
def my_array():
students = list()
total = 0
NumStudents = int(input("Please enter number of students: "))
for i in range(int(NumStudents)):
Student_Name = input("Please enter student name: ")
students.append(Student_Name)
Student_Num = int(input("Please enter student number: "))
students.append(int(Student_Num))
Student_Mark = int(input("Please enter student mark: "))
students.append(int(Student_Mark))
total += Student_Mark
listA = students
new_list = np.array_split(listA, NumStudents)
for item in new_list:
print(list(item))
#print(list(item[0]))
print('Average Student Mark is', str(total / NumStudents))
def find_min(students):
minimum = students[0]
for x in students[1:]:
if x < minimum:
minimum = x
return minimum
find_min(students)
my_array()
but I keep getting traceback:
Traceback (most recent call last):
File "C:\Users\MMaseko\PycharmProjects\utopiaWasteDisposalPlant\iterable.py", line 37, in <module>
my_array()
File "C:\Users\MMaseko\PycharmProjects\utopiaWasteDisposalPlant\iterable.py", line 36, in my_array
find_min(students)
File "C:\Users\MMaseko\PycharmProjects\utopiaWasteDisposalPlant\iterable.py", line 33, in find_min
if x < minimum:
TypeError: '<' not supported between instances of 'int' and 'str'
Some of the values in your list are strings (student names) and some are integers (students' grades). You can't find a minimum if some of the values you are comparing aren't integers.
def my_array():
students = []
total = 0
n = int(input())
for _ in range(n):
name = input()
number = int(input())
mark = int(input())
total+=mark
students.append((mark, number, name))
return min(students)[2]
This solves your problem. There are some other parts of your code that you have, but I haven't implemented them because you didn't require it.
Here's a code that works, The find_min() function had an error due to an indentation problem with the return minimum
def my_array():
students = list()
total = 0
NumStudents = int(input("Please enter number of students: ")) #resizing array to the number of students entered by user
for i in range(int(NumStudents)): #add student info to the empty array (students = list() )
Student_Num = int(input("Please enter student number: "))
students.append(int(Student_Num))
Student_Mark = int(input("Please enter student mark: "))
students.append(int(Student_Mark))
total += Student_Mark
listA = students
new_list = np.array_split(listA, NumStudents) #spliting array to number of students as entered by user
for item in new_list:
print(list(item))
#print(list(item[0]))
print('Average Student Mark is', str(total / NumStudents))
def find_min(students): #determining lowest student mark
minimum = students[0]
for x in students[1:]:
if x < minimum:
minimum = x
return minimum
print("The lowest student mark is", str(find_min(students))) #printing lowest student mark
find_min(students)
my_array()
Your find_min function is not good. I mean if there is no element lower than the first one, it won't return anything. But even if there is one - all the others won't be considered at all - the return statement will break the loop.
The rigth way:
def find_min(students):
minimum = students[0]
for x in students[1:]:
if x < minimum:
minimum = x
return minimum

How to assign grade from a list in python

I am creating a program that takes users and stores them in list and takes their marks and afterwards it grades them. I've done the user input part and the marks as well along with the average of them but I am struggling to give them grade and print it along with their name so it will be like Name marks Grade. If anyone can help me with this I'll be thankful greatly here is my code
students=[]
for i in range (2):
x=(input("Enter Student Name. \n"))
students.insert(i,x)
i+=1
print(students)
grades = []
for student in students:
grade = eval(input(f"Enter the grade for {student}: "))
grades.append(grade)
result = list(zip(students, grades))
print(result)
average = sum(grades) / len(grades)
print ( "Average is: " + str(average))
total = sum(grades)
# print ("Total is: " + str(total))
print("Highest marks", max(list(zip(grades, students))))
print("Lowest marks", min(list(zip(grades, students))))
## To do assign grades to each according to their marks
I am struggling to give them grade and print it along with their name so it will be like Name marks Grade.
Look at the below - it uses zip and loop
names = ['Jack','Dan']
grades = [67,86]
def _get_grade(grade):
if grade < 50:
return 'C'
elif grade >= 50 and grade <= 75:
return 'B'
else:
return 'A'
for name,grade in zip(names,grades):
print(f'Name: {name} - Grade: {_get_grade(grade)}')

How do I use user input to determine how the program works?

this is my first post here on stackoverflow and this is my first time using Python to make a program. I want to make a program to ask the user to input names of students then be able to add more information to that student as the program continues to run. Ideally I want the user to choose how many students there are, choose how many rounds are played, give them names, assign scores, and at the end output the final score and the averages of each student.
This is what I have currently:
name_students = list()
num_students = input("How many students?: ")
competitionRounds = input("How many rounds: ")
score_students = list ()
myList = name_students
for i in range(1, int(num_students) + 1):
name_students.append(input("Student Name: "))
print(name_students)
for i in range (1, int(competitionRounds)):
print(input(name_students [0] + " Total Score for each round: "))
This is how the program runs:
How many student?: 3 #Have the user input answers to these questions
How many rounds?: 2
Student Name: Nick
Student Name: Bob
Student Name: Lisa
Nick Total Score for each round:
I was trying to get it to ask for every name listed like
Nick Total Score for round 1:
Bob Total score for round 1:
Lisa Total score for round 1:
Nick Total score for round 2:
Etc.
I appreciate anyone who replies.
---Edit---
So I now have problems taking the numbers inputted by the user and adding them together by the name placed by the user.
My expected outcomes is:
Nick Total Score for round 1: 2
Bob Total score for round 1:3
Lisa Total score for round 1:1
Nick Total Score for round 2:2
Bob Total score for round 2:3
Lisa Total score for round 2:1
Nick Total score for all rounds: 4
Etc
Currently my code looks like:
name_students = list()
num_students = input("How many students?: ")
competitionRounds = input("How many rounds: ")
score_students = []
myList = name_students
total_scores = 0, []
for i in range(1, int(num_students) + 1):
name_students.append(input("Student Name: "))
for i in range (1, int(competitionRounds) +1):
for j in range(len(name_students)):
score_students.append(input(name_students [j] + " Total Score for round " + str(i) +": "))
for i in range (1,int(competitionRounds) +1):
for t in range(len(score_students)):
print(name_students + score_students + total_scores)
You need to change the last loop
for i in range (1, int(competitionRounds)):
for j in range(len(name_students)):
score_students.append(input(name_students [j] + " Total Score for round " + str(i) + ": "))
This will ask the user for each student's score for each round, and keep appending that to score_students. You may then manipulate that the way you want.
How many students?: 2
How many rounds: 3
Student Name: A
Student Name: B
['A', 'B']
A Total Score for round 1: 2
B Total Score for round 1: 1
A Total Score for round 2: 2
B Total Score for round 2: 1

Control conditional loop in Python

I'm creating a program that asks for the number of students, then asks for their names.
Ex:
Enter the test scores of the students:
> 4
When I use the same method for grades, it won't work (the final output desired is the name of the student next to the grade). My second loop doesn't seem to work.
Desired Output:
Enter the test scores of the students: 5
Bob
Tom
Obi
Eli
Brady (only lets me add 5 names)
Enter the test scores of the students:
100
99
78
90
87 (only lets me add 5 grades)
OUTPUT:
Bob 100
Tom 99
Obi 78
Eli 90
Brady 87
Here is the code I have tried:
students = []
scores = []
count = 0
count2 = 0
number_of_students = int(input("Enetr the number of students: "))
while count != number_of_students:
new_student = input()
students.append(new_student)
count = count + 1
if count == number_of_students:
break
print("Enter the test scores of the students: ")
while count2 != count:
new_score = input()
scores.append(new_score)
count2 = count2 + 1
if count == number_of_students:
break
What can I change?
I think this is a case where you're making the problem harder than it is. You don't need to check the end condition both at the while and at the end of the loop -- just once should be fine. Also you don't need a counter for the second loop, you can just loop over the names from the first loop:
students = []
scores = []
count = 0
number_of_students = int(input("Enter the number of students: "))
while count < number_of_students:
new_student = input("Student name: ")
students.append(new_student)
count = count + 1
print("Enter the test scores of the students:")
for name in students:
new_score = input("Score for " + name + ": ")
scores.append(new_score)
But whenever I see parallel arrays like this, an alarm goes off that says you need a better data structure. Perhaps an array of tuples or a dictionary.

Categories