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)}')
Related
Create a program that will calculate a student’s grade in a class that uses a weighted gradebook.
A weighted system uses percentages to determine how much each assignment category is worth. For this project, use the following percentages:
Project Grades = 30% (weight = .30)
Participation Grades = 20% (weight = .20)
Quizzes = 10% (weight = .10)
Exams = 40% (weight = .40)
Grading scale:
A - 90-100
B - 80-89
C - 70-79
D - 60-69
F - Below 60
Note: You will multiply the average for each of the categories by its weight. The final grade is the sum of the calculated weights.
Can't get the code to the allow the user to enter grades in all 4 categories, because it gets stuck on the first category chosen and the loop just goes on and on without stopping asking for a list of scores. I attempted a loop inside a loop, but it only continued on the first one chosen as well. Need multiple grades entered into all 4 categories which is why I need the program to allow the user to enter grades to the other categories not just the one that is chosen first.
print ("Weighted Grade Calculator ")
name = input("Enter the Student's Name ")
number = input("Enter the Student's Number ")
x = input("Enter Assignment type: ")
c = 'y'
#Loop is in case user wants to calculate another students grade
while c=='y' or c=='Y':
if x >= "quiz" or x >= "Quiz":
input_string = input("Enter a list of scores separated by space ")
#Read ints separated by spaces
lst = input_string.split()
print("Calculating sum of element of input list")
#convert strings to ints and convert map to list
lst = list(map(int, lst))
#Find total and avg
total = sum(lst)
avg_1 = total/len(lst)
elif x >= "project" or x >= "Project":
input_string = input("Enter a list of scores separated by space ")
lst = input_string.split()
print("Calculating sum of element of input list")
lst = list(map(int, lst))
total = sum(lst)
avg_2 = total/len(lst)
elif x >= "participation" or x >= "Participation":
input_string = input("Enter a list of scores separated by space ")
lst = input_string.split()
lst = list(map(int, lst))
total = sum(lst)
avg_3 = total/len(lst)
elif x >= "exam" or x >= "Exam":
input_string = input("Enter a list of scores separated by space ")
lst = input_string.split()
print("Calculating sum of element of input list")
lst = list(map(int, lst))
total = sum(lst)
avg_4 = total/len(lst)
else:
print("error, please try again")
#Finds percentage earned from each category
w_quiz = avg_1 * 0.1
w_project = avg_2 * 0.3
w_participation = avg_3 * 0.2
w_exams = avg_4 * 0.4
total = w_project + w_quiz + w_participation + w_exams
if(total >= 90 and total <=100):
grade = 'A'
elif(total >= 80 and total < 90):
grade = 'B'
elif(total >= 70 and total < 80):
grade = 'C'
elif(total >= 60 and total < 70):
grade = 'D'
elif(total >= 0 and total < 60):
grade = 'F'
print ("Student Name: " + str(name))
print ("Student ID: " + str(number))
print ("Total Weighted Score: " + str(total) + "%")
print ("Letter Grade: " + grade)
c = input("Would you like to calculate another grade? (Y or N): ")
Not sure exactly what you are looking for, but this code will take as much input as you give it and at the end will calculate the average of all totals and its respective letter grade:
def calculate_letter(total):
#function calculates letter grade
if(total >= 90 and total <=100):
grade = 'A'
elif(total >= 80 and total < 90):
grade = 'B'
elif(total >= 70 and total < 80):
grade = 'C'
elif(total >= 60 and total < 70):
grade = 'D'
elif(total >= 0 and total < 60):
grade = 'F'
return grade
print ("Weighted Grade Calculator ")
name = input("Enter the Student's Name ")
number = input("Enter the Student's Number ")
totals = [] #initialize list to append scores to
while True:
project = float(input("Enter the percentage for Projects (numbers only): "))
participation = float(input("Enter the percentage for the Participation (numbers only): "))
quizzes = float(input("Enter the percentage for Quizzes (numbers only): "))
exams = float(input("Enter the percentage for the Final Exam (numbers only): "))
w_project = project * 0.3
w_participation = participation * 0.2
w_quiz = quizzes * 0.1
w_exams = exams * 0.4
total = w_project + w_quiz + w_participation + w_exams
grade = calculate_letter(total) #running the function defined above to evaluate letter grade from total
print ("Total Weighted Score: " + str(total) + "%")
print ("Letter Grade: " + grade)
c = input("Would you like to calculate another grade? (Y or N): ").lower()
totals.append(total)
if c == 'y':
#restarts the loop
continue
else:
#exits the loop
break
print(totals)
final_averaged_score = sum(totals) / len(totals) #finding the average of the list
final_grade = calculate_letter(final_averaged_score)
print ("Student Name: " + str(name))
print ("Student ID: " + str(number))
print ("Final Averaged score: " + str(final_averaged_score))
print ("Final letter grade: " + str(final_grade))
I am in need of assistance on a coding question in Python.
I have to calculate a student’s GPA. The program must ask them how many classes they are taking, then ask them to enter the grades for each class and if it is weighted.
The program should then output the averaged GPA including the decimal place, and my main program must call the function.
The question gives a chart for the non-weighted and weighted number scores that correlates with the given letter grade:
gpa chart
Here's what I have so far:
def average (c):
div = c
avg =(1.0*(sum(scores))/div)
#****************MAIN********************
c = input("How many classes are you taking?")
print ("You are taking " + str(c) + " classes.")
x = input("Enter your letter grade.")
w = int(input("Is it weighted? (1 = yes)")
while (c <= 7): #Here it is saying that there is a syntax error because I input a while loop. I don't know why it is saying this.
if (x == A):
if (w == 1):
print ("Your GPA score is 5")
else:
print ("Your GPA score is 4")
elif (x == B):
if (w == 1):
print ("Your GPA score is 4")
else:
print ("Your GPA score is 3")
elif (x == C):
if (w == 1):
print ("Your GPA score is 3")
else:
print ("Your GPA score is 2")
elif (x == D):
if (w == 1):
print ("Your GPA score is 2")
else:
print ("Your GPA score is 1")
elif (x == F):
if ( w == 1):
print ("Your GPA score is 1")
else:
print ("Your GPA score is 0")
scores = []
list.append(x)
average(c)
Any help would be much appreciated! :)
Not sure about what you are asking for, but fixing just after the function definitions might be a good start
Don't
def classes(c):
print ("You are taking " + c + " classes.")
Do
def classes(c):
print ("You are taking " + c + " classes.")
Something like that maybe? I didn't divide anything in classes but I want you to understand the logic:
number_class=None
while number_class==None:# I set-it up number_class=None so it will keep asking the number of classes until you introduce an integer value
try:
number_class=input("How many classes are you taking?")
except:
print "Error, the input should be a number!\n"
total_result=0#your score start from 0
i=0
while i<number_class:# i create a loop to insert the score for each class
grade=raw_input("Enter your letter grade for the %s class." %(str(i+1)))
grade=grade.upper()#convert the grate to upper so you are able to introduce a or A without make too many check
if grade== 'A' or grade== 'B' or grade== 'C' or grade== 'D' or grade== 'F':#if you introduce a correct score we add it to the total sum and we go ahead with next class
i+=1#next iteration
if grade== 'A':
total_result+=4
elif grade== 'B':
total_result+=3
elif grade== 'C':
total_result+=2
elif grade== 'D':
total_result+=1
#elif: grade== 'F':#we can omitt F seeing that it's =0
# total_result+=0
print total_result
else:# if you introduce a wrong input for the grade we ask you again without pass to next iteration
print "Error, the grade should be: A, B, C, D or F!\n"
average="%.2f" % (float(total_result)/float(number_class))#we divided the total score for the number of classes using float to get decimal and converting on 2 decimal places
print "Your GPA is: %s" %(str(average))
Example:
How many classes are you taking?5
Enter your letter grade for the 1 class.A
4
Enter your letter grade for the 2 class.B
7
Enter your letter grade for the 3 class.C
9
Enter your letter grade for the 4 class.A
13
Enter your letter grade for the 5 class.B
16
Your GPA is: 3.20
I try to calculate several properties of students:
the amount of students
the sum of the marks of the students
the lowest, the average and the highest mark they received.
Yet, the variable mark only shows 0.
How can I solve this problem using function but not max() and min()?
mark = 0
a = 0
student = 0
a = int(input("Enter Marks :"))
def maxx():
maxx = 0
for i in range(1, a):
if a> maxx :
maxx = a
return maxx
def minn():
minn = 0
for i in range(1, a):
if a < minn :
minn = a
return minn
while (a >= 0):
mark = mark + a
student = student + 1
a = int(input("Enter Marks :"))
print("Exit")
print("Total students :", student)
print ("The total marks is:", mark)
average = mark/student
print ("The average marks is:", average)
print("The max marks is :", maxx())
print("The min marks is :", minn())
Your code has a lot of problems. One of them is
for i in range(1, a):
This part makes no sense if you want a min or max value. You need to iterate over a list of grades instead.
mark and student are also unnecessary considering they can be replaced by sum and len respectively.
The entire code seems to lack a proper structure. Here's an example implementation. If you are not allowed to use sum or len, you may bring your own mark and student method back, but try not to make a mess and keep it readable:
def maxx(grades):
if (not grades): # if empty, we let the caller know
return None
res = grades[0] # we know the list is not empty
for i in grades:
if i > res:
res = i
return res
def minn(grades):
if (not grades):
return None
res = grades[0]
for i in grades:
if i < res:
res = i
return res
def main():
grades = [] # list of grades
while (True):
grade = int(input("Enter Mark: "))
if (grade < 0): break
grades.append(grade)
student_cnt = len(grades)
total = sum(grades)
print("Exit")
print("Total students :", student_cnt)
print("The total marks is:", total)
print ("The average marks is:", total / student_cnt)
print("The max marks is :", maxx(grades))
print("The min marks is :", minn(grades))
if __name__ == "__main__":
main()
Input/Output:
Enter Mark: 30
Enter Mark: 20
Enter Mark: 10
Enter Mark: 40
Enter Mark: -1
Exit
Total students : 4
The total marks is: 100
The average marks is: 25.0
The max marks is : 40
The min marks is : 10
I have been struggling with this code for a couple hours and am not seeing much in previous questions that relates to it. I've gone down the rabbit hole trying to make it work so I think it's pretty inefficient at this point as well as isn't outputting in the manner I'd like.
I am trying to get user input in the form of multiple grade entries, evaluate them based on grading criteria and then output the students grade in letter form
Any advice on getting it to output correctly or on any better coding techniques would be greatly appreciated. Thanks ahead of time from a novice coder.
Here is my code:
"""10.1 - write a program that reads a list of scores and then
assigns letter grades based on the criteria:
A if score is >= best - 10
B if score is >= best - 20
C if score is >= best - 30
D if score is >= best - 40
F otherwise
"""
grades_input = input("Enter Students scores seperated by a space: ") #get user input of student's grades
grades_list = grades_input.split() #split user input into a list
grades_list_valid = [ int(x) for x in grades_list ] #convert items into integers
number_of_students = []
for i in range(len(grades_list_valid)):
number_of_students.append(i)
for grade in grades_list_valid: #create criteria to assign each letter grade
best_score = max(grades_list_valid) #get the highest grade
if grade >= best_score - 10:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is A")
elif grade >= best_score - 20:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is B")
elif grade >= best_score - 30:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is C")
elif grade >= best_score - 40:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is D")
else:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is F")
and here is the output:
Enter Students scores seperated by a space: 40 55 70 58
Student 3 score is 58 and grade is C
Student 3 score is 58 and grade is B
Student 3 score is 58 and grade is A
Student 3 score is 58 and grade is B
The desired output is:
Student 0 score is 40 and grade is C
Student 1 score is 55 and grade is B
Student 2 score is 70 and grade is A
Student 3 score is 58 and grade is B
Its because your first loop to get i is completing before you make use of i, then i is already 3 when you loop through to print the results. Instead you should get the index in your second loop like this:
grades_input = input("Enter Students scores seperated by a space: ") #get user input of student's grades
grades_list = grades_input.split() #split user input into a list
grades_list_valid = [ int(x) for x in grades_list ] #convert items into integers
best_score = max(grades_list_valid)
for i, grade in enumerate(grades_list_valid):
letter_grade = "F"
if grade >= best_score - 10:
letter_grade = "A"
elif grade >= best_score - 20:
letter_grade = "B"
elif grade >= best_score - 30:
letter_grade = "C"
elif grade >= best_score - 40:
letter_grade = "D"
else:
letter_grade = "F"
print("Student {} score is {} and grade is {}".format(i, grade, letter_grade))
and then eliminate the first loop.
After looking again at the question i also noticed you were grabbing the "best score" from the same list every time, which would make each student have the same score. Im guessing you just want to grab the score for each student. I updated my answer to work for that - not sure if it was your desired behaviour.
Edit: looked at it again and realized what you were doing with best score, fixed my answer here's the results when i run it:
Enter Students scores seperated by a space: "70 80 93 77 31"
Student 0 score is 70 and grade is C
Student 1 score is 80 and grade is B
Student 2 score is 93 and grade is A
Student 3 score is 77 and grade is B
Student 4 score is 31 and grade is F
grades_input = input("Enter Students scores seperated by a space: ") #get user input of student's grades
grades_list = grades_input.split() #split user input into a list
grades_list_valid = [ int(x) for x in grades_list ] #convert items into integers
number_of_students = []
for i in range(len(grades_list_valid)):
number_of_students.append(i)
i=0
for grade in grades_list_valid: #create criteria to assign each letter grade
best_score = max(grades_list_valid) #get the highest grade
print(i)
if grade >= best_score - 10:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is A")
elif grade >= best_score - 20:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is B")
elif grade >= best_score - 30:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is C")
elif grade >= best_score - 40:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is D")
else:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is F")
i+=1
this the correct way,in your code i=3 which is why you are getting the same answer for each student
As Quinn said, your top loop iterates over i, which finishes at i = 3. Future references to i will yield 3.
Some things I noticed:
1) What you can do is check their grade at the same time as you loop through.
2) Additionally, you don't need to find the maximum each loop, you can get it once outside the loop and then reference it inside.
3) Finally, it looks quite ugly when you have all of these print statements, it makes it much nicer to read if you assign the grade in the if statements and then print once with the found grade, like so:
best_score = max(grade_list_valid)
for i in range(len(grades_list_valid)):
curr_score = grades_list_valid[i]
if(curr_score >= best_score - 10):
grade = "A"
...
print("Student {} score is {} and grade is {}".format(i, curr_score, grade))
There are some further small optimisations you can make as well. For example, the list comprehension you have:
[ int(x) for x in grades_list ]
Is the classic case for a map:
map(int, grades_list)
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.