im working on a python grade calculator for class where i can only use if/else/elif statements to drop the lowest score and then average the scores. this is what i have so far but i dont know how to determine the lowest score from the input using ONLY if/else statements. any help is appreciated!
# Input
sName = str(input("Name of person that we are calculating the grades for: "))
iTest1 = int(input("Test 1: "))
iTest2 = int(input("Test 2: "))
iTest3 = int(input("Test 3: "))
iTest4 = int(input("Test 4: "))
sDrop = str(input("Do you wish to drop the lowest grade Y or N? "))
# Test If Statements
if iTest1 <= 0:
print("Test scores must be greater than 0.")
raise SystemExit
if iTest2 <= 0:
print("Test scores must be greater than 0.")
raise SystemExit
if iTest3 <= 0:
print("Test scores must be greater than 0.")
raise SystemExit
if iTest4 <= 0:
print("Test scores must be greater than 0.")
raise SystemExit
# Drop Lowest If Statements
if sDrop != "Y" or "N" or "y" or "n":
print("Enter Y or N to drop the lowest grade.")
raise SystemExit
# Grade Scale
if score >= 97.0:
grade = "A+"
elif score >= 94.0 and score <= 96.9:
grade = "A"
elif score >= 90.0 and score <= 93.9:
grade = "A-"
elif score >= 87.0 and score <= 89.9:
grade = "B+"
elif score >= 84.0 and score <= 86.9:
grade = "B"
elif score >= 80.0 and score <= 83.9:
grade = "B-"
elif score >= 77.0 and score <= 79.9:
grade = "C+"
elif score >= 74.0 and score <= 76.9:
grade = "C"
elif score >= 70.0 and score <= 73.9:
grade = "C-"
elif score >= 67.0 and score <= 69.9:
grade = "D+"
elif score >= 64.0 and score <= 66.9:
grade = "D"
elif score >= 60.0 and score <= 63.9:
grade = "D-"
else:
grade = "F"
Test the first two to find the smallest of those. Now compare the other two in turn to smallest and update smallest if necessary.
if iTest1 < iTest2:
smallest = iTest1
else:
smallest = iTest2
if iTest3 < smallest:
smallest = iTest3
if iTest4 < smallest:
smallest = iTest4
Of course, all of this is a tedious way to write:
smallest = min([iTest1, iTest2, iTest3, iTest4])
Why wouldn't my for loop work? If I put in 0.85 for grade score it'd print out F and error message instead of B. Why is this?
grade=input('Score Grade:')
fg=float(grade)
for fg in range(0,1):
if fg >= 0.9:
print('A')
elif fg>=0.8:
print('B')
elif fg>=0.7:
print('C')
elif fg>=0.6:
print('D')
else:
print('F')
print('error grade out of range')
quit()
You are misusing the range() function. range() is used to iterate over multiple values, not to validate if a number is in a range. You should instead check that fg greater than or equal to 0, or less than or equal to 1. Like this:
grade=input('Score Grade:')
fg=float(grade)
if 0 > fg or 1 < fg:
print('error grade out of range')
quit()
if fg >= 0.9:
print('A')
elif fg>=0.8:
print('B')
elif fg>=0.7:
print('C')
elif fg>=0.6:
print('D')
else:
print('F')
you are doing this for one time you dont need to use a loop
you can do this
grade = float(input('Score Grade:'))
if grade < 1 and grade > 0:
if grade > 0.9:
print('A')
elif grade >= 0.8:
print('B')
elif grade >= 0.7:
print('C')
elif grade >= 0.6:
print('D')
elif grade < 0.6:
print('F')
else:
print('error grade out of range')
quit()
You do not need to use for operator neither range. The simplest solution is:
'''
grade = input("Enter number:")
try:
grade = float(grade)
except:
grade = -1
if grade >= 0.9:
print("A")
elif grade >= 0.8:
print("B")
elif grade >= 0.7:
print("C")
elif grade >= 0.6:
print("D")
elif grade < 0.6:
print("F")
else:
print("Error!")
quit()
'''
Does anyone know how to get the average of the random grades? She wants to generate just random grades and get the average but I had also done one where you input the grades on your end but I don't think that's what she was looking for.
import random
grade = random.randint(50,100)
grade2 = random.randint(50,100)
grade3 = random.randint(50,100)
grade4 = random.randint(50,100)
grade5 = random.randint(50,100)
if int(grade) >= 90 and int(grade) <= 100:
print ("Grade 1:", grade, "A")
elif int(grade) >= 80 and int(grade) <=90:
print("Grade 1:", grade, "B")
elif int(grade) >= 70 and int(grade) <= 80:
print("Grade 1:", grade, "C")
elif int(grade) > 60 and int(grade) <=70:
print("Grade 1:", grade,"D")
elif int(grade) <= 60:
print("Grade 1:", grade, "F")
if int(grade2) >= 90 and int(grade2) <= 100:
print ("Grade 2:", grade2, "A")
elif int(grade2) >= 80 and int(grade2) <=90:
print("Grade 2:", grade2, "B")
elif int(grade2) >= 70 and int(grade2) <= 80:
print("Grade 2:", grade2, "C")
elif int(grade2) > 60 and int(grade2) <=70:
print("Grade 2:", grade2,"D")
elif int(grade2) <= 60:
print("Grade 2:", grade2, "F")
if int(grade3) >= 90 and int(grade3) <= 100:
print ("Grade 3:", grade3, "A")
elif int(grade3) >= 80 and int(grade3) <=90:
print("Grade 3:", grade3, "B")
elif int(grade3) >= 70 and int(grade3) <= 80:
print("Grade 3:", grade3, "C")
elif int(grade3) > 60 and int(grade3) <=70:
print("Grade 3:", grade3,"D")
elif int(grade3) <= 60:
print("Grade 3:", grade3, "F")
if int(grade4) >= 90 and int(grade4) <= 100:
print ("Grade 4:", grade4, "A")
elif int(grade4) >= 80 and int(grade4) <=90:
print("Grade 4:", grade4, "B")
elif int(grade4) >= 70 and int(grade4) <= 80:
print("Grade 4:", grade, "C")
elif int(grade4) > 60 and int(grade4) <=70:
print("Grade 4:", grade4,"D")
elif int(grade4) <= 60:
print("Grade 4:", grade4, "F")
if int(grade5) >= 90 and int(grade5) <= 100:
print ("Grade 5:", grade5, "A")
elif int(grade5) >= 80 and int(grade5) <=90:
print("Grade 5:", grade5, "B")
elif int(grade5) >= 70 and int(grade5) <= 80:
print("Grade 5:", grade5, "C")
elif int(grade5) > 60 and int(grade5) <=70:
print("Grade 5:", grade5,"D")
elif int(grade5) <= 60:
print("Grade 5:", grade5, "F")
If you find yourself putting a number after variable names, that's a big sign that you might want to use a list (or, in general, an iterable) to store the elements:
grades = [51, 62, 87, 57]
There are several ways to generate a list of random grades numpy.random.rand is one, but an easier way for a beginner might be to just start off with a list like
grades = []
and then create a loop to append the grades. Python provides a sum function to sum up all of the values in your list, and a len function to compute the number of elements in the list. With these functions, you can compute the grades.
You can also use a list comprehension to generate the grades if you're familiar with them.
If you go ahead and create the grades list, your code can be adapted as follows:
for idx, grade in enumerate(grades, 1):
label = f"Grade {idx}"
if int(grade) >= 90 and int(grade) <= 100:
print (label, grade, "A")
elif int(grade) >= 80 and int(grade) <=90:
print(label, grade, "B")
elif int(grade) >= 70 and int(grade) <= 80:
print(label, grade, "C")
elif int(grade) > 60 and int(grade) <=70:
print(label, grade,"D")
elif int(grade) <= 60:
print(label, grade, "F")
Here enumerate(grades, 1) returns an index and an element from grades, starting at 1 because we specify the 1 here.
As Kraigolas pointed out, you can eliminate a lot of copied+pasted code by putting the grades into a list instead of 5 individual variables, and using enumerate to generate the "grade 1", "grade 2", etc.
The easiest way to get an average is to use statistics.mean, which gives you the mean (average) value of any list that you pass to it:
>>> grades = [70, 80, 85]
>>> import statistics
>>> statistics.mean(grades)
78.33333333333333
I'd also suggest putting the logic to determine a letter grade into a function, like this:
def letter_grade(grade: float) -> str:
assert 0 <= grade <= 100
for score, letter in [
(90, "A"),
(80, "B"),
(70, "C"),
(60, "D"),
]:
if grade >= score:
return letter
return "F"
Now you can put it all together with a very small amount of code:
import random
import statistics
grades = [random.randint(50, 100) for _ in range(5)]
for n, grade in enumerate(grades, 1):
print(f"Grade {n}: {letter_grade(grade)}")
print(f"Average: {letter_grade(statistics.mean(grades))}")
Sample output:
Grade 1: F
Grade 2: A
Grade 3: A
Grade 4: A
Grade 5: A
Average: B
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
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. :)