The goal is to build a program to convert scores from a '0 to 1' system to an 'F to A' system:
- If
score >= 0.9
would print 'A'
- If
score >= 0.8
would print 'B'
- 0.7, C
- 0.6, D
- And any value below that point, print F
This is the way to build it and it works on the program, but it's somewhat repetitive:
if scr >= 0.9:
print('A')
elif scr >= 0.8:
print('B')
elif scr >= 0.7:
print('C')
elif scr >= 0.6:
print('D')
else:
print('F')
I would like to know if there is a way to build a function so that the compound statements wouldn't
be as repetitive.
I'm a total beginner, but would something in the lines of :
def convertgrade(scr, numgrd, ltrgrd):
if scr >= numgrd:
return ltrgrd
if scr < numgrd:
return ltrgrd
be possible?
The intention here is that later we can call it by only passing the scr, numbergrade and letter grade as arguments:
convertgrade(scr, 0.9, 'A')
convertgrade(scr, 0.8, 'B')
convertgrade(scr, 0.7, 'C')
convertgrade(scr, 0.6, 'D')
convertgrade(scr, 0.6, 'F')
If it would be possible to pass fewer arguments, it would be even better.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…