1

Im really stumped. Im writing a program for my teacher (Im using Python 3 btw), so that he can give this code to students to calculate their grade instead of waiting for their report card. I'm only a beginner so try and keep the answer simple please :D

Okay here is the problem. I have all of the inputs needed for the code. the inputs work like this. A = 5 B = 4 C = 3 D = 2 E = 1. If you got straight A's you'd get 50 points, and so on, but if it results in say, 35 points all the grade calculators will crash. Because if its >30 its a B, but if its >20 its a C, But >20 and >30 print at the same time. Because they both execute if the result is greater than 30. And i dont know how to make it so that it will print say, "B" if it is 31 to 40.

This is the code

a = eval(input())
b = eval(input())
c = eval(input())
d = eval(input())
e = eval(input())
f = eval(input())
g = eval(input())
h = eval(input())
i = eval(input())
j = eval(input())

average = a + b + c + d + e + f + g + h + i + j
print(average)

if average >41:
    print(" Grade A ")

if average >31: 
    print(" Grade B")

if average >21:
    print(" Grade C")

if average >11 :
    print(" Grade D")

if average >0 
    print(" Grade E")

Any Help Would Be Greatly Appreciated! Thanks.

2
  • How about an if-elif construct?
    – bezmax
    Apr 26, 2012 at 8:24
  • eval(input()) is a really bad way of doing this. Why not use int(input)? Apr 26, 2012 at 15:03

2 Answers 2

5

The best way to do what you want is to define a group of data. if/elif blocks work, but are ungainly, and require a lot of extra typing:

import sys

mark_boundaries = [("A", 41), ("B", 31), ("C", 21), ("D", 11), ("C", 0)]

try:
    marks = []
    for i in range(10):
        marks.append(int(input()))
except ValueError:
    print("You entered an invalid mark, it must be a number.")
    sys.exit(1)

average = sum(marks) #I'd just like to note the misleading variable name here.
#average = sum(marks)/len(marks) #This would be the actual average mark.
print(average)

for mark, boundary in mark_boundaries:
    if average >= boundary:
        print("Grade "+mark)
        break #We only want to print out the best grade they got.

Here we use a list of tuples to define our boundaries. We check from highest to lowest, breaking out if we match (so it doesn't 'fall through' to the lower scores).

Likewise, you can see that I have used a loop to gather the data in for the marks. A good sign that you are doing something in an inefficient way while programming is that you have copy and pasted (or typed out again and again) a bit of code. This generally means you need to put it in a loop, or make it a function. I also used int(input()) rather than eval(input()), which is a safer option, as it doesn't allow execution of anything the user wants. It also allows us to nicely catch the ValueError exception if the user types in something which isn't a number.

Note that an enterprising individual might look at a list of pair tuples and think a dict would be a good replacement. While true in most cases, in this case, we need to order to be right - in a dict the order is arbitrary, and might lead to us checking lower scores first, giving them a lower mark than they deserve.

Just as a note, it is entirely possible to do

if 31 < average < 41: #Equivalent to `if 31 < average and average < 41:`
    print("Grade B")

In python. That said, for this usage, this would mean a lot more typing than using a list and loop or if/elif.

4
  • Thanks for the Answer! However it was a bit too complicated for me. The Answer Below Helped me the most. Im sure when i get further into python i will understand what you are implying Apr 27, 2012 at 0:25
  • @JosephSmith What was it you didn't understand? Apr 27, 2012 at 0:27
  • Alot of the Commands ( statements, functions..) Whatever you call them. I've never seen before. You obviously know a great deal about Programming, however i've only really just begun. Thanks for the answer anyway :D Will be sure to come back to this when im a bit more experienced ! Apr 27, 2012 at 6:10
  • 1
    @JosephSmith You learn by seeing new things and figuring out what they do, not by ignoring new things because you don't know about them.
    – Marcin
    Sep 16, 2013 at 15:53
3

Basically, this is what you want:

if average >41:
    print(" Grade A ")
elif average >31: 
    print(" Grade B")
elif average >21:
    print(" Grade C")
elif average >11 :
    print(" Grade D")
elif average >0 
    print(" Grade E")
else
    print("You broke the system")

elif is short for else if, so it executes ONLY if the previous if/elif block was not executed.

1
  • Max, I've tried that Elif solution before, but you did it a different way, i'll give it a go. Apr 26, 2012 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.