Reputation: 109
I am a beginner programmer and am self learning python programming at home from a book. I am currently learning about strings. There is a question there which i solved it but I was thinking if there are other easier ways to solve it.
Q. A certain professor gives 100-point exams that are graded on the scale 90-100:A, 80-89:B, 70-79:C, 60-69:D, <60:F. Write a program that accepts an exam score as input and prints out the corresponding grade.
def main():
## making a list from 0-100
num = list(range(0,101))
## asking for the exam points
points = int(input("Enter the exam points 0-100: "))
## iterating num 0-60
for i in num[0:60]:
if i == points:
print("Your corresponding grade is F.")
## iterating num 60-70
for i in num[60:70]:
if i == points:
print("Your corresponding grade is D.")
## iterating num 70-80
for i in num[70:80]:
if i == points:
print("Your corresponding grade is C.")
## iterating num 80-90
for i in num[80:90]:
if i == points:
print("Your corresponding grade is B.")
## iterating num 90-100
for i in num[90:101]:
if i == points:
print("Your corresponding grade is A.")
main()
Upvotes: 0
Views: 196
Reputation: 59146
Yes, there is a much better way of writing that.
Given that you have an int, which is the score, all you have to do is compare it to the boundaries that determine the grade (using <
or >
).
points = int(input("Enter the exam points 0-100: "))
if points < 60:
grade = 'F'
elif points < 70:
grade = 'D'
elif points < 80:
grade = 'C'
elif points < 90:
grade = 'B'
else:
grade = 'A'
print("Your corresponding grade is", grade)
To make your code clearer, you can put the comparison logic into a function that returns the grade for a given score.
def calculate_grade(score):
if score < 60:
return 'F'
if score < 70:
return 'D'
if score < 80:
return 'C'
if score < 90:
return 'B'
return 'A'
def main():
points = int(input("Enter the exam points 0-100: "))
grade = calculate_grade(points)
print("Your corresponding grade is", grade)
Upvotes: 4
Reputation: 11
Yes there is, I would have used this approach
def main():
points = int(input("Enter the exam points 0-100: "))
if points >= 60:
print("Your corresponding grade is F.")
elif points > 60 and points < 70:
print("Your corresponding grade is D.")
elif points > 70 and points < 80:
print("Your corresponding grade is C.")
elif points > 80 and points < 90:
print("Your corresponding grade is B.")
elif points > 90:
print("Your corresponding grade is A.")
else:
print("Error: invalid value")
Upvotes: -3
Reputation: 495
There's still an easier and more concise way to do this. Try this:
points = int(input("Enter the exam points 0-100: "))
if 0 < points <= 60:
print "Your grade is F"
elif 60 < points <= 70:
print "Your grade is E"
elif 70 < points <= 80:
print "Your grade is D"
[and so on...]
Benefits are:
if
sUpvotes: 3
Reputation: 373
#Maybe using dictionaries is more fun :)
marks = {'F':60, 'D':70,'C':80,'B':90}
points = int(input("Enter the exam points 0-100: "))
for key,value in marks.items():
if points<=value:
print("Your corresponding grade is ", key)
break
print("Your corresponding grade is A")
Upvotes: 1
Reputation: 1175
def main():
points = int(input('Enter the exam points:'))
if points >= 90:
print('Your corresponding grade is A')
elif 80 <= points <= 90:
print('Your corresponding grade is B.')
elif 70 <= points <= 80:
print('Your corresponding grade is C.')
elif 60 <= points <= 70:
print('Your corresponding grade is D.')
elif 0 <= points <= 60:
print('Your corresponding grade is F.')
Upvotes: 0
Reputation: 743
your solution is pretty not optimal and could be solved really nice using some basic logic, like below, which will perform better and would be more readable:
## asking for the exam points
point = int(input("Enter the exam points 0-100: "))
point_set = 'FEDCBA'
if point == 100:
print('A')
else:
print(point_set[point // 10 - 5])
code is using simple logic, once point is less than 60, 59 // 10 evaluates to 5 and 5 - 5 = 0, so grade at index 0 will be used - F. In case point is 100, this is some edge case, and that's why I'm using special if, for other cases there is simple math, which is self explanatory.
Upvotes: -1
Reputation: 149075
It is correct (gives the expected grade) and terribly inefficient:
In addition you use a pure linear (unstructured) design.
You could:
Code example
def points_to_grade(points):
limits = [90, 80, 70, 60]
grades = ['A', 'B', 'C', 'D']
for i,limit in enumerate(limits):
if points >= limit:
return grade[i]
return 'F'
def main():
## asking for the exam points
points = int(input("Enter the exam points 0-100: "))
## convert to grade
grade = points_to_grade(points)
print("Your corresponding grade is {}.".format(grade))
Upvotes: 1
Reputation: 476
I do not code in python so I might not use right syntax but this is pretty much same in all languages. Iterating trough all values is bad practice. You just need few if statements (or switch/case), something like this:
if i<=100 and i>=90:
print("Grade is A")
if i<=89 and i>=80:
print("Grade is B")
etc....
Upvotes: 0