My code:
def askTestScores():
test1 = int(input("Enter test 1 score: "))
test2 = int(input("Enter test 2 score: "))
test3 = int(input("Enter test 3 score: "))
test4 = int(input("Enter test 4 score: "))
test5 = int(input("Enter test 5 score: "))
testScores = (test1,test2,test3,test4,test5)
return testScores
def determine_grade(testScores):
for i in range(testScores):
if i >= 90 and i <= 100:
return "A"
elif i >= 80 and i <= 89:
return "B"
elif i >= 70 and i <= 79:
return "C"
elif i >= 60 and i <= 69:
return "D"
else:
return "F"
def displayGrades(testScores):
print("Score\t\t Grade")
print("-----------------------")
print(f"{testScores[0]}\t\t {determine_grade(testScores[0])}")
print(f"{testScores[1]}\t\t {determine_grade(testScores[1])}")
print(f"{testScores[2]}\t\t {determine_grade(testScores[2])}")
print(f"{testScores[3]}\t\t {determine_grade(testScores[3])}")
print(f"{testScores[4]}\t\t {determine_grade(testScores[4])}")
x = askTestScore
displayGrades(x)
This is only printing "F" when I use the range function in the loop for determine_grade function. What I want is, to print each grade individually, but when I remove the range function I am getting type error: saying int object not iterable. How can I solve this?
instead of range() Use:
for i in testscores :
Why do you need for in determine_grades ?
I think you meant this
def determine_grades(testScore):
if testScore >= 90 and testScore <= 100:
return "A"
elif testScore >= 80 and testScore <= 89:
return "B"
elif testScore >= 70 and testScore <= 79:
return "C"
elif testScore >= 60 and testScore <= 69:
return "D"
else:
return "F"
def displayGrades(testScores):
print("Score\t\t Grade")
print("-----------------------")
for testScore in testScores:
print(f"{testScore}\t\t {determine_grades(testScore)}")
Here you loop testScores in displayGrades to pass one score at a time to determine_grade and print them.
Because if you used for loop in determine_grades then you need to return a list of grades and not String.
You can improve make your code by appending the inputs to a list instead of tuple
def askTestScores():
testScores = []
for i in range(5):
score = int(input(f"Enter test {i + 1} score: "))
testScores.append(score)
return testScores
EDIT:
If you need tuple you can convert the list to tuple
def askTestScores():
testScores = []
for i in range(5):
score = int(input(f"Enter test {i + 1} score: "))
testScores.append(score)
testScores = tuple(testScores)
return testScores
the easiest way to do it is to include each item of your tuple in a list and remove the range function:
def askTestScores():
test1 = int(input("Enter test 1 score: "))
test2 = int(input("Enter test 2 score: "))
test3 = int(input("Enter test 3 score: "))
test4 = int(input("Enter test 4 score: "))
test5 = int(input("Enter test 5 score: "))
testscores = ([test1],[test2],[test3],[test4],[test5])
return testscores
def determine_grade(testscores):
for i in testscores:
if i >= 90 and i <= 100:
return "A"
elif i >= 80 and i <= 89:
return "B"
elif i >= 70 and i <= 79:
return "C"
elif i >= 60 and i <= 69:
return "D"
else:
return "F"
def displayGrades(testscores):
print("Score\t\t Grade")
print("-----------------------")
print(f"{testscores[0]}\t\t {determine_grade(testscores[0])}")
print(f"{testscores[1]}\t\t {determine_grade(testscores[1])}")
print(f"{testscores[2]}\t\t {determine_grade(testscores[2])}")
print(f"{testscores[3]}\t\t {determine_grade(testscores[3])}")
print(f"{testscores[4]}\t\t {determine_grade(testscores[4])}")
x = askTestScores()
displayGrades(x)
displayGrades(x)
Related
def determine_grade(score):
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
elif score >= 70:
return 'C'
elif score >= 60:
return 'D'
else:
return 'F'
def calc_average(test1, test2, test3, test4, test5):
avg = sum((test1, test2, test3, test4, test5))/5
return avg
def main():
test1 = int(input('Enter score 1: '))
test2 = int(input('Enter score 2: '))
test3 = int(input('Enter score 3: '))
test4 = int(input('Enter score 4: '))
test5 = int(input('Enter score 5: '))
print('Average of the Test Scores')
print()
print(f'{"score":>5}{"numeric grade":>20}{"letter grade":>15}')
print('----------------------------------------')
test1_score = determine_grade(test1)
print(f'{"score 1:":<5}{50:>10}{test1_score:>15}')
test2_score = determine_grade(test2)
print(f'{"score 2:":<5}{50:>10}{test2_score:>15}')
test3_score = determine_grade(test3)
print(f'{"score 3:":<5}{50:>10}{test3_score:>15}')
test4_score = determine_grade(test4)
print(f'{"score 4:":<5}{50:>10}{test4_score:>15}')
test5_score = determine_grade(test5)
print(f'{"score 5:":<5}{50:>10}{test5_score:>15}')
main()
You should call the method, just like the other function that you are calling.
line 26 of your code:
# ...
print('Average of the Test Scores')
avg_score = calc_average(test1, test2, test3, test4, test5)
print(avg_score)
# ...
def getGradeInfo():
numStudent = int(input('Total number of students: '))
getGrades = input(f'Enter {numStudent} scores: ')
gradeListNum = getGrades.split(' ')
maxScore = int(max(gradeListNum[0:numStudent]))
gradeListLetter = []
for grade in gradeListLetter:
if int(grade) >= maxScore - 10:
gradeListLetter.append('A')
elif int(grade) >= maxScore - 20:
gradeListLetter.append('B')
elif int(grade) >= maxScore - 30:
gradeListLetter.append('C')
elif int(grade) >= maxScore - 40:
gradeListLetter.append('D')
else:
gradeListLetter.append('F')
a = 1
while a<= numStudent:
print(f'Student{a} score is {gradeListNum[a-1]} and grade is')
a+=1
getGradeInfo()
I can get the input for the number of students in the code but can't figure out how to assign the letter to each of the student's grades for the print statement
As Adam Oppenheimer said,
for grade in gradeListLetter:
should be
for grade in gradeListNum:
Also,
print(f'Student{a} score is {gradeListNum[a-1]} and grade is')
should be
print(f'Student {a}: score is {gradeListNum[a - 1]} and grade is {gradeListLetter[a - 1]}')
You are iterating over gradeListLetter which is empty, try iterating over gradeListNum instead.
You also aren't printing the letter grade in your print statement.
I'm trying to complete my menu driven program; however, I can't get this code to function properly. the goal is to get information from my previous code then get the evaluated output here. It keeps asking to execute my previous function before the menu pops up. get_list is the previous function that gets the list of numbers from the user.
previous code
def get_list(score_list=None):
if score_list is None:
score_list = []
n = int(input('How many scores do you want to enter? '))
for n in range(1, n + 1):
score = float(input('Enter score #{}: '.format(n)))
while True:
if 0 > score or score > 100:
print('Invalid score entered!!\n', 'Score should be a value between 0 and 100')
score = float(input('Enter score #{}: '.format(n)))
continue
else:
score_list.append(score)
break
return score_list
pass
problem code
def evaluate_list():
score_list = get_list()
lowest_score = min(score_list)
score_list.remove(lowest_score)
score_average = sum(score_list) / len(score_list)
if 90 < score_average <= 100:
grade = 'A'
elif 70 < score_average <= 80:
grade = 'B'
elif 40 < score_average <= 60:
grade = 'C'
elif score_average > 20 and score_average >= 40:
grade = 'D'
else:
grade = 'F'
print('----------Results--------------')
print('Lowest Score : ', lowest_score)
print('Modified List: ', score_list)
print('score Average: ', f'{score_average:.2f}')
print('Grade : ', grade)
evaluate_list()
I am working on a python lettering assignment.
90 or above is an A and so on and so on for the rest of the letter grades; but when a value is inputted as a negative number, I need the code to do nothing other than display an error.
This is what i tried so far:
#Design a Python program to assign grade to 10 students
#For each student, the program first asks for the user to enter a positive number
#A if the score is greater than or equal to 90
#B if the score is greater than or equal to 80 but less than 90
#C if the score is greater than or equal to 70 but less than 80
#D if the score is greater than or equal to 60 but less than 70
#F is the score is less than 60
#Ihen the program dispalys the letter grade for this student.
#Use while loop to repeat the above grade process for 10 students.
keep_going = 'y'
while keep_going == "y":
num = float(input("Enter a number: "))
if num >= 90:
print("You have an A")
elif num >= 80:
print("You have an 3")
elif num >= 70:
print("You have an C")
elif num >= 60:
print("You have an D")
elif (num < 60 and <= 0:
print ("You have an F")
else:
print("lnvalid Test Score.")
Original screenshot
I see three problems, all in the same line:
elif (num < 60 and <= 0:
Syntax: num < 60 and <= 0 is not a valid expression; should be num < 60 and num <= 0
Logic: num <= 0 is not what you want, it should be num >= 0
Syntax: you missed a closing bracket ).
If you change those, it should work.
grade = int(input("Enter Score:"))
print "FFFFFDCBAA"[grade//10] if grade >= 0 else "ERROR!!!!"
you just have to change your elif for below 60.
keep_going = 'y'
while keep_going == "y":
num = float(input("Enter a number: "))
if num >= 90:
print("You have an A")
elif num >= 80:
print("You have an 3")
elif num >= 70:
print("You have an C")
elif num >= 60:
print("You have an D")
elif 60 > num >= 0:
print ("You have an F")
else:
print("lnvalid Test Score.")
I'm fairly new to programming and have only been doing so for a month. Currently I'm trying to get user input, store it in a list, and pass that list into a function. I'm having trouble using the list as an argument for my function (the last line of code). Thank in advance!
grade_list = []
percentages = 0
while True:
percentages = input("Enter some numbers here: ")
if percentages == "done":
break
grade_list.append(percentages)
print(grade_list)
def gpaCalc(marks):
gpaList = []
for grade in marks: #sorts data
if grade <= 49.99:
grade = 0.00
elif 50 <= grade <= 52.99:
grade = 0.70
elif 53 <= grade <= 56.99:
grade = 1.00
elif 57 <= grade <= 59.99:
grade = 1.30
elif 60 <= grade <= 62.99:
grade = 1.70
elif 63 <= grade <= 66.99:
grade = 2.00
elif 67 <= grade <= 69.99:
grade = 2.30
elif 70 <= grade <= 72.99:
grade = 2.70
elif 73 <= grade <= 76.99:
grade = 3.00
elif 77 <= grade <= 79.99:
grade = 3.30
elif 80 <= grade <= 84.99:
grade = 3.70
elif 85 <= grade <= 89.99:
grade = 3.90
elif 90 <= grade <= 100:
grade = 4.00
gpaList.append(grade) #gathers data into list
gpaList.sort()
return gpaList
print (gpaCalc(PROBLEM))
You can pass a list as you would pass it normally into any function, just always make sure you are accessing the items in the list by indexing correctly, rather than calculating the whole list. Use the following instead:
def gpaCalc(marks):
gpaList = []
for grade in marks[0]: #sorts data
if grade <= 49.99:
grade = 0.00
elif 50 <= grade <= 52.99:
grade = 0.70
elif 53 <= grade <= 56.99:
grade = 1.00
elif 57 <= grade <= 59.99:
grade = 1.30
elif 60 <= grade <= 62.99:
grade = 1.70
elif 63 <= grade <= 66.99:
grade = 2.00
elif 67 <= grade <= 69.99:
grade = 2.30
elif 70 <= grade <= 72.99:
grade = 2.70
elif 73 <= grade <= 76.99:
grade = 3.00
elif 77 <= grade <= 79.99:
grade = 3.30
elif 80 <= grade <= 84.99:
grade = 3.70
elif 85 <= grade <= 89.99:
grade = 3.90
elif 90 <= grade <= 100:
grade = 4.00
gpaList.append(grade) #gathers data into list
gpaList.sort()
return gpaList
grade_list = []
percentages = 0
while True:
percentages = input("Enter some numbers here: ")
if percentages == "done":
break
grade_list.append(percentages)
print(gpaCalc(grade_list))
keep your check for "done" as is. if it is not done, then convert float.
while True:
percentages = input("Enter some numbers here and 'done' to exit:")
if percentages == "done":
break
try:
grade_list.append(float(percentages))
except ValueError:
pass
sorting...
for grade in marks: #sorts data
.....
gpaList.append(grade) #gathers data into list
#also, sort outside the loop, when done, not each time.
gpaList.sort()
return gpaList
Right before the last print line, define your list of marks, e.g. marks = [70, 68, 50, 89, ...] and pass it to gpaCalc in your function call:
print(gpaCalc(marks))
Note that Python convention says you should not use camel case in your identifiers; use underlines instead: gpa_calc
Edit: I missed the point of the question! To get the user's inputs, use a loop:
def get_user_input():
grades = []
while True:
# take input
value = ... # figure it out
if value == 'q':
break
try:
# do basic validation here
grades.append(int(value))
# might be a good idea to check the range too…
except ValueError:
print("This is not a valid grade!")
return grades
If you would like an explanation, leave a comment!