Grade is calculated incorrectly - python

The code bellow seems to be semantically functional only for the first student, as his grade is calculated correctly. For the second student it does not calculate the expected grade . Another problem is how to print the iterator from the for loop so that I get the prompt:
Enter name of assessment 1:
Enter name of assessment 2:
etc...
assessment=[]
weight=[]
assign_number = int(input('How many assessment : '))
def main():
for i in range(assign_number):
asses_name =input('Enter name of assessment: ')
assessment.append(asses_name)
asses_value =int(input('How many marks is the ' + assessment[i] + ' worth : '))
weight.append(asses_value)
if sum(weight)!= 100:
del weight[:]
print("the total is not 100, please re enter")
main()
else:
student_details()
def calculatedgrade(score):
if 100<= score:
grade = 'High Distiction'
elif 80 <= score <= 100:
grade = 'High Distiction'
elif 70 <= score <= 79:
grade = 'Distiction'
elif 60 <= score<= 69:
grade ='Credit'
elif 50 <= score <= 59:
grade = 'Pass'
elif score <=50:
grade = 'Fail'
return grade
def student_details():
student_name =[]
student_mark =[]
marks=[]
total_mark=[]
student_number = int(input('How many students : '))
for n in range(student_number):
name = input('What is the name of student: ')
student_name.append(name)
for m in range(assign_number):
mark = input('what did ' + format(student_name[n])+' get out of
' +format(weight[m]) +' in the ' +assessment[m] +' ?')
marks.append(mark)
score = int(marks[m])/int(weight[m])*100
student_mark.append(score)
calculatedgrade(score)
print(mark,' out of ',weight[m],'is a ',(calculatedgrade(score))
main ()

For printing the counter: print your current display string concatenated to i. Like so:
stringForInput = 'Enter name of assessment '
for i in range(assign_number):
stringForInput = stringForInput + str(i+1) + ' :'
asses_name =input(stringForInput)

Related

Program to calculate grade

I am writing a python program to calculate the test grade of 5 students and it will show what grade they get by entering their test score. But I can't get a result like this. Can someone help me out, please!
5
88
92
76
100
66
Grade Distribution
A: 2
B: 1
C: 1
D: 1
F: 0
Average Grade: 84.4
`def read_test_scores():
print("Enter number of students: ")
num = int(input())
print("Enter the next grade: ")
score1 = int(input())
score2 = int(input())
score3 = int(input())
score4 = int(input())
score5 = int(input())
sum = (score)
tavge = sum / 5.0
return tavge
def get_letter_grade():
if 90 <= n <= 100:
grade = 'A'
elif 80 <= n <= 89:
grade = 'B'
elif 70 <= n <= 79:
grade = 'C'
elif 60 <= n <= 69:
grade = 'D'
elif n<60:
grade = 'F'
return grade
def print_comment(grade):
a=0
b=0
c=0
d=0
f=0
if grade == 'A':
a += 1
print ('A: ' +str(a))
elif grade == 'B':
b += 1
print ('B: ' +str(b))
elif grade == 'C':
c += 1
print ('C: ' +str(c))
elif grade == 'D':
d +=1
print ('D: ' +str(d))
elif grade == 'F':
f +=1
print ('F: ' +str(f))
tavge = read_test_scores()
print ('Grade Distribution')
print_comment(grade)
print ("Test Average is: " + str(tavge))`
Here's what I think you were going for. I had to make several changes that I can clarify if needed... Mostly you needed to store the scores and grades as an array of values instead of individual ones. When you need to, for instance, tally up the number of A's, B's, etc. you just loop through the array and add up the occurrences.
def read_test_scores():
print("Enter number of students: ")
num = int(input())
scores = []
print("Enter the next grade: ")
for score in range(num):
score = int(input())
while score < 0 or score > 100:
print('Score should be between 0 and 100, try again:')
score = int(input())
scores.append(score)
sum_scores = sum(scores)
tavge = sum_scores / num
return tavge, scores
def get_letter_grade(n):
if 90 <= n <= 100:
grade = 'A'
elif 80 <= n <= 89:
grade = 'B'
elif 70 <= n <= 79:
grade = 'C'
elif 60 <= n <= 69:
grade = 'D'
elif n<60:
grade = 'F'
return grade
def print_comment(grades):
a=0
b=0
c=0
d=0
f=0
for grade in grades:
if grade == 'A':
a += 1
elif grade == 'B':
b += 1
elif grade == 'C':
c += 1
elif grade == 'D':
d +=1
elif grade == 'F':
f +=1
print ('A: ' +str(a))
print ('B: ' +str(b))
print ('C: ' +str(c))
print ('D: ' +str(d))
print ('F: ' +str(f))
tavge, scores = read_test_scores()
grades = []
for score in scores:
grades.append(get_letter_grade(score))
print ('Grade Distribution')
print_comment(grades)
print ("Test Average is: " + str(tavge))
This is my idea
Get number of students
Create total_score, or any Something you need to record
for in Students
Get score then add in total_score
total_score = 0
for i in range(student_number) :
score = int(input())
total_score += score
# get_letter_grade
average = total_score / student_number
print ("Test Average is" + str())

Append is adding the sum instead of adding to a list python

I tried to append but whenever I would try to print it down in the main(), it would print the average but added up.
def letter_grade(test_score):
if int(test_score) >= 90:
grade = 'A'
elif int(test_score) >= 80:
grade = 'B'
elif int(test_score) >= 70:
grade = 'C'
elif int(test_score) >= 60:
grade = 'D'
else:
grade = 'F'
return grade
def calc_avg_grade(test_score):
average = []
func_sum = sum(test_score)
avg = func_sum/5.00
average.append(avg)
average.reverse()
return average
def main():
grades = []
i = 0
outfile = open('studentgrades.txt', 'w')
while i < 4:
name = input("Enter the student name: ")
for x in range(5):
score = float(input("Enter number grade: "))
grades.append(score)
gradle = letter_grade(score)
print(str(score) + ' - ' + gradle)
avg_grade = calc_avg_grade(grades)
avgg = avg_grade
print(name + "'s average grade is: " + str(avgg))
outfile.write(name + ', ' + str(avg_grade) + "\n")
txtcontents = outfile.read()
print(txtcontents)
if __name__ == "__main__":
main()
The part where it runs the append that I have the problem at is:
print(name + "'s average grade is: " + str(avgg))
This is def main().
I think that you are not reinitialising grades:
def main():
# grades = [] # Not Here!
i = 0
outfile = open('studentgrades.txt', 'w')
while i < 4:
name = input("Enter the student name: ")
grades = [] # here instead
for x in range(5):
score = float(input("Enter number grade: "))
grades.append(score)
gradle = letter_grade(score)
print(str(score) + ' - ' + gradle)
avg_grade = calc_avg_grade(grades)
avgg = avg_grade
print(name + "'s average grade is: " + str(avgg))
outfile.write(name + ', ' + str(avg_grade) + "\n")
txtcontents = outfile.read()
print(txtcontents)
This way you get a new empty grades for each student and avg_grade will be the right average for that student.

How can I make it so the user only has 2 tries to enter valid input?

print('Hello, welcome to your grade calculator.')
GradeCount = 0
totalGrades = 0.0
moreStudent = 'y'
while moreStudent == 'y' or moreStudent == 'Y':
grade = float(input('Enter a grade or a -1 to end: '))
while grade != -1:
if grade > 100 or grade < 0:
print('Invalid input. Please enter a value between 1 and 100.')
grade = float(input('Enter the next grade or -1 to end: '))
continue
totalGrades = totalGrades + grade
GradeCount = GradeCount + 1
if 90 <= grade <=100:
print('You got an A. Thats awesome.')
print('Number of grades entered: ',GradeCount)
print('Class total: ',totalGrades)
elif 80 <= grade < 90:
print('You got a B. Good job.')
print('Number of grades entered: ',GradeCount)
print('Class total: ',totalGrades)
elif 70 <= grade < 80:
print('You got a C. Thats fine I guess.')
print('Number of grades entered: ',GradeCount)
print('Class total: ',totalGrades)
elif 60 <= grade < 70:
print ('You got a D. Not very good.')
print('Number of grades entered: ',GradeCount)
print('Class total: ',totalGrades)
elif grade < 60:
print ('You got an F. You fail.')
print('Number of grades entered: ',GradeCount)
print('Class total: ',totalGrades)
grade = float(input('Enter the next grade or -1 to end: '))
moreStudent = input('Are you a new student and ready to enter your
grades? y or n: ')
print ('Number of grades entered:', GradeCount)
print ('Class total:',totalGrades)
print ('Class grade average:', format(totalGrades / GradeCount, '.2f'))
How can I make it so the user only has 2 tries before the program issues an error message then clears the screen and starts over? Also how can I make it clear the screen every time there is a new user?
The most basic modification you could use to the current code is to add a counter, other methods can be used as well
while moreStudent == 'y' or moreStudent == 'Y':
grade = float(input('Enter a grade or a -1 to end: '))
count = 0
while grade != -1:
if grade > 100 or grade < 0:
count += 1
if count == 2:
moreStudnet = 'n'
break
else:
print('Invalid input. Please enter a value between 1 and 100.')
grade = float(input('Enter the next grade or -1 to end: '))
continue
totalGrades = totalGrades + grade
GradeCount = GradeCount + 1

Python program to calculate student grades [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This is my first program in Python and I am having some trouble so forgive me if I have some simply syntax issues.
I am writing a program that calculates a student's final score based on one exam grade worth 60% and 7 other test scores worth a combined 40% of the final grade. The user is asked to input the one exam score then asked to input the 7 test scores which are read in a loop. The letter grade is then determined from the final score calculated from the exam and tests. After that a grade comment is printed corresponding to the letter grade given to the student. This is my code so far:
def read_test_scores() :
print("ENTER STUDENT ID: ")
id = int(input())
print("ENTER EXAM SCORE: ")
exam = int(input())
print("ENTER ALL TEST SCORES: ")
score1 = int(input())
score2 = int(input())
score3 = int(input())
score4 = int(input())
score5 = int(input())
score6 = int(input())
score7 = int(input())
sum = (score1 + score2 + score3 + score4 + score5 + score6 + score7)
tavge = sum/7
return tavge
def compute_final_score(tavge, exam) :
final_score = 0.4 * tavge + 0.6 * exam
return final_score
def get_letter_grade(final_score) :
if 90 <= final_score <= 100:
grade = 'A'
elif 80 <= final_score <= 89:
grade = 'B'
elif 70 <= final_score <= 79:
grade = 'C'
elif 60 <= final_score <= 69:
grade = 'D'
else:
grade = 'F'
return grade
def print_comment(grade) :
if grade = 'A':
print "COMMENT: Very Good"
elif grade = 'B':
print "COMMENT: Good"
elif grade = 'C':
print "COMMENT: Satisfactory"
elif grade = 'D':
print "COMMENT: Need Improvement"
elif grade = 'F'
print "COMMENT: Poor"
read_test_scores()
print "TEST AVERAGE IS: " + str(tavge)
compute_final_score()
print "FINAL SCORE IS: " + str(final_score)
get_letter_grade(final_score)
print "LETTER GRADE IS: " + str(grade)
print_comment(grade)
Here's my answer. The code should run. Notes are inserted as comments.
# NOTE: I haven't checked whether your math is right, or
# if the computed values are correct. I did however get your
# script to work.
def read_test_scores():
print("ENTER STUDENT ID: ")
id = int(input())
print("ENTER EXAM SCORE: ")
exam = int(input())
print("ENTER ALL TEST SCORES: ")
score1 = int(input())
score2 = int(input())
score3 = int(input())
score4 = int(input())
score5 = int(input())
score6 = int(input())
score7 = int(input())
sum = (score1 + score2 + score3 + score4 + score5 + score6 + score7)
tavge = sum / 7.0
# NOTE: if you want to use any variables from this function,
# then you have to "bring them outside" by "returning"
# them. Here, I return the values tavge, id, and exam. I noticed
# that bringing out "exam" is necessary since you'll
# be using it later on.
return tavge, id, exam
def compute_final_score(tavge, exam):
final_score = 0.4 * tavge + 0.6 * exam
return final_score
def get_letter_grade(final_score):
if 90 <= final_score <= 100:
grade = 'A'
elif 80 <= final_score <= 89:
grade = 'B'
elif 70 <= final_score <= 79:
grade = 'C'
elif 60 <= final_score <= 69:
grade = 'D'
else:
grade = 'F'
return grade
def print_comment(grade):
# NOTE `=` is for assignment. We use it when we want to
# tell python to make a variable mean something. For example:
# a = "some_name" basically means that when we call a, it would
# return the string "some_name".
# What you want to use here is `==` which is the equality operator.
# This checks whether or thing are equal.
if grade == 'A':
print("COMMENT: Very Good")
elif grade == 'B':
print("COMMENT: Good")
elif grade == 'C':
print("COMMENT: Satisfactory")
elif grade == 'D':
print("COMMENT: Need Improvement")
elif grade == 'F':
print("COMMENT: Poor")
# NOTE 1: you need to assign the function results to a
# variable (or variables), otherwise, the result or return value
# will go nowhere and you can't use it
tavge, id, exam = read_test_scores()
print "TEST AVERAGE IS: " + str(tavge)
# NOTE 2: variable names do not have to be the same as
# the name in their respective functions. Here, you can see
# that it will still run even if I changed the variable
# name final_score to my_variable. Although, of course, using
# final_score would still work.
# NOTE 3: the final_score function requires 2 inputs,
# namely tavge and exam. This basically means that you have to feed
# it with these 2 values for it to work. I took the
# tavge and exam variables as the results from your read_test_scores
# function
my_variable = compute_final_score(tavge, exam)
print("FINAL SCORE IS: " + str(my_variable))
grade = get_letter_grade(my_variable)
print("LETTER GRADE IS: " + str(grade))
print_comment(grade)
# FINAL NOTE: I haven't commented regarding coding style etc (like say
# for instance, there are best practices regarding variable names
# within functions, that is, if they should be similar to variable names
# outside the function), but regardless, the code is a good start. I
# would also advise you to try to narrow down your question first
# before posting. This can be done by running your code, and searching
# the internet for the particular erro messages, and if you're still stuck,
# ask here on stackoverflow.
there are many errors in your code, some of them are in the comment, but the most critical part is that you use global and local variables incorrectly
here is and example of fixing your code using the correct way to use global variables.
https://repl.it/repls/SorrowfulOddballSongbird
tavge = 0
exam = 0
sid = 0
final_score = 0
grade = ''
def read_test_scores() :
global sid
print("ENTER STUDENT ID: ")
sid = int(input())
global exam
print("ENTER EXAM SCORE: ")
exam = int(input())
print("ENTER ALL TEST SCORES: ")
score1 = int(input())
score2 = int(input())
score3 = int(input())
score4 = int(input())
score5 = int(input())
score6 = int(input())
score7 = int(input())
total = (score1 + score2 + score3 + score4 + score5 + score6 + score7)
global tavge
tavge = total/7
#return tavge
def compute_final_score() :
global final_score
final_score = 0.4 * tavge + 0.6 * exam
#return final_score
def get_letter_grade() :
global grade
if 90 <= final_score <= 100:
grade = 'A'
elif 80 <= final_score <= 89:
grade = 'B'
elif 70 <= final_score <= 79:
grade = 'C'
elif 60 <= final_score <= 69:
grade = 'D'
else:
grade = 'F'
#return grade
def print_comment() :
if grade == 'A':
print("COMMENT: Very Good")
elif grade == 'B':
print ("COMMENT: Good")
elif grade == 'C':
print ("COMMENT: Satisfactory")
elif grade == 'D':
print ("COMMENT: Need Improvement")
elif grade == 'F':
print ("COMMENT: Poor")
read_test_scores()
print ("TEST AVERAGE IS: " + str(tavge))
compute_final_score()
print ("FINAL SCORE IS: " + str(final_score))
get_letter_grade()
print ("LETTER GRADE IS: " + str(grade))
print_comment()
but you should consider using parameters instead using globals
As several people have mentioned you need to use == for comparison, you also are missing a colon after one of your if/else.
This is my take on your code. Keep in mind that this doesn't have and tests to make sure someone is actually entering in a number for a test score instead of text
"sum" is also the name of a built in function to Python, which sums up anything you provide it.
def read_test_scores():
scores = []
num_tests = 7
print("ENTER ALL TEST SCORES: ")
for i in range(num_tests):
score = input("Test " + str(i + 1) + ":")
scores.append(int(score))
return sum(scores) / num_tests
def compute_final_score(average, exam_score):
score = 0.4 * average + 0.6 * exam_score
return score
def get_letter_grade(finalized_score):
if 90 <= finalized_score <= 100:
letter_grade = 'A'
elif 80 <= finalized_score <= 89:
letter_grade = 'B'
elif 70 <= finalized_score <= 79:
letter_grade = 'C'
elif 60 <= finalized_score <= 69:
letter_grade = 'D'
else:
letter_grade = 'F'
return letter_grade
def print_comment(letter_grade):
if letter_grade == 'A':
print("COMMENT: Very Good")
elif letter_grade == 'B':
print("COMMENT: Good")
elif letter_grade == 'C':
print("COMMENT: Satisfactory")
elif letter_grade == 'D':
print("COMMENT: Need Improvement")
elif letter_grade == 'F':
print("COMMENT: Poor")
def get_student_id():
print("ENTER STUDENT ID: ")
identity = int(input())
return identity
def get_exam_score():
print("ENTER EXAM SCORE: ")
exam_score = int(input())
return exam_score
if __name__ == '__main__':
student_id = get_student_id()
exam = get_exam_score()
tavge = read_test_scores()
print("TEST AVERAGE IS: " + str(tavge))
final_score = compute_final_score(tavge, exam)
print("FINAL SCORE IS: " + str(final_score))
grade = get_letter_grade(final_score)
print("LETTER GRADE IS: " + str(grade))
print_comment(grade)

determining average grades and displaying letter grades

I'm working on a program for class that finds the average of 5 entered test scores then displays the letter grades relevant to each letter score. letter score is a 10 point system ( A = 90-100 B = 80-89, etc)
This is what I've put together so far but it doesn't seem to recognize "avg" in the syntax. any suggestions?
def main():
while true:
grade = int(input('Enter grade: '))
total += grade
avg = calc_average(total)
abc_grade = determine_grade(grade)
print('Average grade is: ' avg)
print('Letter grades for entered grades are: ' abc_grade)
def calc_average(total):
return total / 5
def determine_grade(grade):
if grade >= 90 and <= 100:
return 'A'
elif grade >= 80 and <= 89:
return 'B'
elif grade >= 70 and <= 79:
return 'C'
elif grade >= 60 and <= 69:
return 'D'
else:
return 'F'
main()
use:
print('Average grade is: '+str(avg))
print('Letter grades for entered grades are: '+abc_grade)
or
print('Average grade is: %.2f'%(avg))
print('Letter grades for entered grades are: %s'%(abc_grade))
_list = []
def calc_average(total):
return total / 5
def determine_grade(grade):
if grade >= 90 and grade <= 100:
return 'A'
elif grade >= 80 and grade <= 89:
return 'B'
elif grade >= 70 and grade <= 79:
return 'C'
elif grade >= 60 and grade <= 69:
return 'D'
else:
return 'F'
while True:
grade = int(input('Enter grade: '))
_list.append(grade)
avg = calc_average(sum(_list))
abc_grade = ' '.join([determine_grade(mark) for mark in _list])
if len(_list) > 5:
break
print('Average grade is: ', avg)
print('Letter grades for entered grades are: ', abc_grade)
def main():
print("This is a program which displays the grade from a score")
print("")
grade = eval(input("What is the value of the score : "))
print("")
if 90 <= grade <= 100:
print("Your get an A")
elif 80 <= grade <= 89:
print("Your get a B")
elif 70 <= grade <= 79:
print("Your get a C")
elif 60 <= grade <= 69:
print("Your get a D")
else:
print("Your get an F")
main()
This Worked for me.. A few minor changes except your code is working fine.
def main():
total = 0;avg = 0;abc_grade = 0
def calc_average(total):
return total / 5
def determine_grade(grade):
if 90 <= grade <= 100:
return 'A'
elif 80 <= grade <= 89:
return 'B'
elif 70 <= grade <= 79:
return 'C'
elif 60 <= grade <= 69:
return 'D'
else:
return 'F'
while(True):
grade = int(input('Enter grade: '))
total += grade
avg = calc_average(total)
abc_grade = determine_grade(grade)
print('Average grade is: ' + str(avg))
print('Letter grades for entered grades are: ' + str(abc_grade))
main()
Hope you can find out differences. :)

Categories

Resources