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])
Related
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())
I am trying to print a grade with the score value for instance 0.85
At first I tried like this below:
score = input("Enter Score: ")
float(score)
if 0.0 <= score <= 1.0:
if score >= 0.9:
print('A')
elif score >= 0.8:
print('B')
elif score >= 0.7:
print('C')
elif score >= 0.6:
print('D')
else:
print('F')
else:
print("ERROR")
But this returns an ERROR message.
Does anyone know why?
I know I can do this like: score = float(input("Enter Score: "))
What the difference?
you didn't store your float(score) in a variable . you can do it this way .
score_string = input("Enter Score: ")
score = float(score_string)
if 0.0 <= score <= 1.0:
if score >= 0.9:
print('A')
elif score >= 0.8:
print('B')
elif score >= 0.7:
print('C')
elif score >= 0.6:
print('D')
else:
print('F')
else:
print("ERROR")
You can use isinstance(score, float) or type(score).__name__ to check, for example:
score = input("Enter Score: ")
print(type(score).__name__) # str
print(isinstance(score, float))
And you can use the method of eval that parsed and evaluated the variable as a Python expression.
for cur_exp in ('12345', '"abc"', '[1, 3, 5]', '("a", "b", "c")'):
var = eval(cur_exp)
print(type(var).__name__) # int, str, list, tuple
By the way, I think this way is more readable, just for your reference
while 1:
score = float(input("Enter Score: "))
rank = (print('error'), exit()) if not 0.0 <= score <= 1.0 else \
'A' if score >= 0.9 else \
'B' if score >= 0.8 else \
'C' if score >= 0.7 else 'D'
print(rank)
I have a python code, it cannot get the correct output. the code is shown as follows:
score = int(input("Please input a score: "))
grade = ""
if score < 60:
grade = "failed"
elif score < 80: # between 60 and 80
grade = "pass"
elif score < 90:
grade = "good"
else:
grade = "excellent"
print("score is (0), level is (1)".format(score,grade))
Can anyone tell me where is the problem? Thank you so much!
You should change your if statement to:
if score <= 60:
grade = "failed"
elif score <= 80 and score > 60: # between 60 and 80
grade = "pass"
elif score <= 90 and score > 80:
grade = "good"
elif score > 90: #Assuming there is no max score since before, you left the last statement as an else.
grade = "excellent"
else: #Not necessary but always nice to include.
print("Not a valid score, please try again.")
And, as #loocid commented, change print("score is (0), level is (1)".format(score,grade)) to print("score is {0}, level is {1}".format(score,grade))
Though I prefer
print("score is " + str(score) + ", level is " + str(grade))
I am a beginner with python and I am working with this project that will print the result of the students.I am done with nearly everything execpt the percentage.See,in my code,the program will only print the percentage of the last person's marks.I need to make it so that it calculates percentage for everyone individually and then print it at the end.Your help will be much appreciated.Thanks
T_marks = 1100
data = {}
while True:
ask = input("What do you want? ")
if ask == "y":
name = input("Enter your name: ")
marks = int(input("Enter marks: "))
data[name] = marks
percentage =(marks / T_marks) * 100
elif ask == "print":
for (key,value) in data.items():
print(key,"::",value)
if percentage > 90:
print("Passed with A grade")
elif percentage >= 70 and percentage < 90:
print("Passed with B grade")
elif percentage >= 60 and percentage < 70:
print("Passed with C grade")
elif percentage >= 50 and percentage < 60:
print("passed with D Grade")
else:
print("You failed")
else:
print("Your work has ended")
break
You need to compute percentage under the print case, this should get you what you want:
T_marks = 1100
data = {}
while True:
ask = input("What do you want? ")
if ask == "y":
name = input("Enter your name: ")
marks = int(input("Enter marks: "))
data[name] = marks
elif ask == "print":
for (key,value) in data.items():
# NOTE percentage is under the case when user asks for print
percentage =(value / T_marks) * 100
print(key,"::",value)
if percentage > 90:
print("Passed with A grade")
elif percentage >= 70 and percentage < 90:
print("Passed with B grade")
elif percentage >= 60 and percentage < 70:
print("Passed with C grade")
elif percentage >= 50 and percentage < 60:
print("passed with D Grade")
else:
print("You failed")
else:
print("Your work has ended")
break
Also two hints: This code will output "You failed" if someone got a grade of 90. You need to set equality at 90 for one of the cases. Also python has simplified comparisons where and is not needed. Here is a simplified version, and corrected for case of 90 to get an A grade:
T_marks = 1100
data = {}
while True:
ask = input("What do you want? ")
if ask == "y":
name = input("Enter your name: ")
marks = int(input("Enter marks: "))
data[name] = marks
elif ask == "print":
for (key,value) in data.items():
percentage =(value / T_marks) * 100
print(key,"::",value)
if percentage >= 90:
print("Passed with A grade")
elif 90 > percentage >= 70:
print("Passed with B grade")
elif 70 > percentage >= 60:
print("Passed with C grade")
elif 60 > percentage >= 50:
print("passed with D Grade")
else:
print("You failed")
else:
print("Your work has ended")
break
The input() method reads a string, but you cannot convert e. g. "4 4 4 5" to int. Method split() without arguments creates a list of the words in string as follow:
"4 5 5" -> ["4", "5", "5"]
Change your input to:
marks_string = input("Enter marks: ")
marks = [int(mark) for mark in marks_string.split()] # convertion to int
And change calculate of percentage:
percentage =(sum(marks) / T_marks) * 100
Indentation, fixed in edit:
T_marks = 1100
data = {}
while True:
ask = input("What do you want? ")
if ask == "y":
name = input("Enter your name: ")
marks = int(input("Enter marks: "))
data[name] = marks
percentage =(marks / T_marks) * 100
elif ask == "print":
for (key,value) in data.items():
print(key,"::",value)
if percentage > 90:
print("Passed with A grade")
elif percentage >= 70 and percentage < 90:
print("Passed with B grade")
elif percentage >= 60 and percentage < 70:
print("Passed with C grade")
elif percentage >= 50 and percentage < 60:
print("passed with D Grade")
else:
print("You failed")
else:
print("Your work has ended")
break
>>> What do you want? y
>>>Enter your name: Alex
>>>Enter marks: 12
>>>What do you want? y
>>>Enter your name: Michael
>>>Enter marks: 22
>>>What do you want? print
>>>Alex :: 12
>>>You failed
>>>Michael :: 22
>>>You failed
>>>What do you want?
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. :)