I'm writing a python program that drops the lowest of four test scores. The program first prompts the user to enter their first four test scores, then the program should drop the lowest test score and take the average of the remaining three test scores. The program should then print the final letter grade. This is what I have so far and I keep receiving an error when dropping the lowest score.
#Enter four test scores as percentages (%)
test1 = int(input("Enter grade 1: 90"))
test2 = int(input("Enter grade 2: 80"))
test3 = int(input("Enter grade 2: 70)")
test4 = int(input("Enter grade 2: 80)")
#Drop lowest test score
print("The average, with the lowest score dropped" )
total =(test1 + test2 + test3)
#Calculate average
def calc_average(total):
return total /3
#Grade scale
def determine_score(grade):
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >=70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
#Calculate final letter grade
print("The final grade is")
main()
I tried to write a program with the things that I understood from you.
Here is the explanation:
Taking four grades from user.
Dropping the lowest one.
Taking average.
Giving a letter grade according to the average.
Here is the code:
test1 = int(input("Enter grade 1: "))
test2 = int(input("Enter grade 2: "))
test3 = int(input("Enter grade 3: "))
test4 = int(input("Enter grade 4: "))
x = min(test1,test2,test3,test4)
total = float(test1 + test2 + test3 + test4 - x)
avg = total / 3
print("Your average is " + str(avg))
def determine_letter(grade):
letter =""
if grade >= 90:
letter = "A"
elif grade >= 80:
letter = "B"
elif grade >= 70:
letter = "C"
elif grade >= 60:
letter = "D"
else:
letter = "F"
print("The final letter grade is " + letter)
determine_letter(avg)
Related
I am trying to figure out how to use the 2 out of 3 scores that were inputted. Please see the sample output below!
num_students = int(input("Enter the number of students: "))
scores = input(f"Enter {num_students} scores: ")
scores = [int(score) for score in scores.split()]
best_score = max(scores)
for i in range(num_students):
if scores[i] >= best_score - 10:
grade = "A"
elif scores[i] >= best_score - 20:
grade = "B"
elif scores[i] >= best_score - 30:
grade = "C"
elif scores[i] >= best_score - 40:
grade = "D"
else:
grade = "F"
print("Student", i, "score is", scores[i], "and grade is", grade)
For instance, if you were asked to input 2 scores but entered 3 instead, it would only pick the first 2 scores.
Also, when less scores are inputted than requested, it just asks to enter again. Please see examples below. Thank you!
students: 2
2 score(s) are: 20 50 60
Student 1 score is 20 and grade is B
Student 2 score is 50 and grade is A
students: 2
Enter 2 score(s): 30
Enter 2 score(s): 50 90
Student 1 score is 50 and grade is A
Student 2 score is 90 and grade is B
So I need to make a program that grades 10 students individually, then displays the average grade for all 10 students.
I think this is how the grading should look, but im not sure how to set up a count on the number of times its graded, or how to set up the average function. Help would be most welcome. I am a horrible coder.
score = int(input("Enter a score between 0 and 100: "))
if score >=89:
print("The grade is an A")
elif score>=79:
print("The grade is a B")
elif score>=69:
print("The grade is a C")
elif score>=59:
print("The grade is a D")
else:
print("The grade is a F")
You can try using a counter variable that goes from 0-9 and use a while loop to check and increment that counter value and after each loop calculates the average and keeps doing this till the last value has been entered
Counter = 0
average = 0
while (Counter <= 10):
score = int(input("Enter a score between 0 and 100: "))
if score >= 89:
print("The grade is an A")
elif score >= 79:
print("The grade is a B")
elif score >= 69:
print("The grade is a C")
elif score >= 59:
print("The grade is a D")
else:
print("The grade is a F")
Counter = Counter + 1
average = (score + average)
average = average/Counter
print("Average of all 10 students:",average)
So right now the code is configured specifically for just 10 loops but you can give the user the power to determine this by suggesting a value to end the loop and the while loop will check for this value and when the loop sees the value it exits the loop and gives a value
Also with each loop there is a counter value that keeps increasing and an average value that holds the sum of the values entered and when the loop exits and average is calculated by dividing the sum by the counters and this gives the results
This should do the trick.
total = 0
for i in range(10):
score = int(input("Enter a score between 0 and 100: "))
if score >= 89:
print("The grade is an A")
elif score >= 79:
print("The grade is a B")
elif score >= 69:
print("The grade is a C")
elif score >= 59:
print("The grade is a D")
else:
print("The grade is a F")
total += score
print("Average of all 10 students:", total/10)
In the for loop you get the score of each student and then add it to the total of all students. When the loop ends you divide it with the total number of students so 10.
total score / total number of students total / 10
You can define a function that finds the average score. Store all the scores in a list and then pass the list as an argument in the function
def avg(scores): #assuming scores is the name of the list
avg = sum(scores)/len(scores)
return "Average score is {}".format(avg)
Or
return f'Average score is {avg}' #for python3
Not sure I understand what you mean by number of times it is graded but I think you can include a while loop and a variable that increases by 1 every time it grades.
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'm extremely new to python, so this code is probably a complete mess, but the issue I'm having is that the code is executing the first 'if' statement no matter what average is calculated (ie. the average calculated is a 76.0, but it's still executing an 'A' grade, rather than a 'C' grade). What am I doing wrong?
Also, just for information's sake, the code is supposed to have the user input four test scores and the code has to randomize a score for the fifth test in range of 70-85, calculate and return the average of all five test scores and assign a grade based off of the average.
def rand():
import random
num5 = random.randint(70,85)
calcAverage(num5)
def calcAverage(num5):
num1 = float(input("Enter the first test score: "))
num2 = float(input("Enter the second test score: "))
num3 = float(input("Enter the third test score: "))
num4 = float(input("Enter the fourth test score: "))
print("\nThe random test score generater on your behalf was", num5)
total = num1 + num2 + num3 +num4 + num5
average = total // 5
letterGrade(average)
def letterGrade(average):
if average >= 90.00 and average <= 100.00:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is A.")
elif average >= 80.00 and average <= 89.99:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is B.")
elif average >= 70.00 and average <= 79.99:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is C.")
elif average >= 60.0 and average <= 69.99:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is D.")
else:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is F.")
rand()
I have been at this for a while, and I am stuck. I have to write a program that asks the user for each score and the average test score. I need the functions calcAverage which accepts five test scores and returns the average of the scores. determineGrade which accepts a score and returns the grade as a string based off the standard grading scale (90-100 is an A, 80-89 is a B, 70-79 is a C, etc). But I also need a function for getValidScore which prompts and reads a single valid test score and once the score is has been input it returns to the calling module. And finally isInValidScore is a function that checks whether a passed test score is in range from 0-100 and returns a Boolean letting me know if it's valid.
Here's what I have:
def main():
test1 = int(input('Enter test grade 1: '))
test2 = int(input('Enter test grade 2: '))
test3 = int(input('Enter test grade 3: '))
test4 = int(input('Enter test grade 4: '))
test5 = int(input('Enter test grade 5: '))
grade = (test1 + test2 + test3 + test4 + test5)
input('press enter to continue')
print(calc_average)
print(determine_grade)
print(getValidScore)
print(isInvalidScore)
return;
def calcAverage():
grade / 5
return averageScore
def determinGrade(score):
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'
def getValidScore():
grade = int(input('input score'))isInvalidScore(score)
while isInvalidScore == false:
print('ERROR')
grade = int(input('input correct score'))
return score
def isInvalidScore(score):
status = True
if score < 0 or score > 100:
status = True
else: status = False
return status
main()
So I added returns and when I run the program, I get this:
Enter test grade 1: 99
Enter test grade 2: 88
Enter test grade 3: 77
Enter test grade 4: 66
Enter test grade 5: 55
press enter to continue
function calc_average at 0x02BAD228>
function determine_grade at 0x02BAD270>
function getValidScore at 0x02BAD2B8>
function isInvalidScore at 0x02BAD300>
There are a few things wrong here:
You're printing out the function itself which is represented by the location in memory, not the result of execution. Add parentheses to the ends of your calls to print out the result.
You should be calling getValidScore and return an integer result instead of reading in input without validation (see main).
Some of your functions take parameters, but you aren't passing any (hint: determinGrade, isInvalidScore).
Some of your functions should take parameters (hint: calcAverage).
calcAverage should return a real number with the actual average. (hint: return grade/5)
Your while loop should start with while isInvalidScore(grade), making the call on the previous line unnecessary.
In main, you're calling an undefined function (determine_grade, is it a typo?)
That's all I can see on a quick look of it. Good luck!
The answer from #tdlive aw'sum is actualy really good (I just upvoted it). Read his comments and work with the stuff. I will supply you with a correction implementation.. I hope you study the aspects you dont understand but take away some good parts from it. Best of luck.
isValidScore = lambda n: 0 <= n <= 100
calcAverage = lambda *args: sum(args)/len(args)
determinGrade = lambda s: ['F', 'D', 'C', 'B', 'A'][max(0, (min(60, 99)/10)-5)]
def getValidScore():
while True:
try:
if isValidScore(int(raw_input('Input score: '))):
return score
except ValueError:
pass
print('Score has to be an integer in the range 0 to 100')
if __name__ == "__main__":
scores = []
for _ in range(5):
scores.append(getValidScore())
print('\nThe scores are: ' + str(scores))
print('The average is: ' + str(calcAverage(*scores)))
print('The grade is: ' + determinGrade(calcAverage(*scores)))