Harry Potter Harry Potter - 22 days ago 6
Python Question

Grading Scale in Python Without Decision Statements

I am trying to create a program in python 3 that will take a percentage score and print out the letter grade based on the grading scale 90-100:A,80-89:B,70-79:C,60-69:D,<60:F using strings and lists

#gradeScale.py
#This program takes a percent score and tells the user what letter grade it is based on the grading scale

def main():
print("This program takes a percent score and tells the user what letter grade it is based on the grading scale")
gradingScale = ["F","F","F","F","F","F","F","D","C","B","A"]
grade = eval(input("Please enter the percent score: "))


main()

Answer

Sorry for using magic out of Hogwarts but:

grades = 'FEDCBA'
score = int(input('Enter your score: '))
grade = ((score // 10) - 4) * (score // 50) * (100 // (score + 1)) + (score // 100) * 5
print('Grade is', grades[grade])

works from 0 to 199 (90-199 is an A)

I'll try to explain 3rd line.

(score // 10) - 4) helps us find grade from 50 to 99 as index in grades string.

* (score // 50) makes all scores from 0 to 49 evaluate to 0 (index of 'F')

* (100 // (score + 1)) + (score // 100) * 5 makes all scores from 100 to 199 evaluate to 5 (index of 'A')

Kids. Never do this at home! (unless you are having fun)