Ben
Ben

Reputation: 39

How do I print the amount of marks above the grade boundary and marks below the next?

I have to code pretty much correct with printing the correct grade, it just prints grade: U and then the correct grade as well and i don't know why this is.

grades = ["A*", "A", "B", "C", "D", "E", "U"]

mark1 = int(input("Computer systems score /140: "))
mark2 = int(input("Algorithms and programming score /140: "))
mark3 = int(input("Programming project score /70: "))
totalScore = mark1 + mark2 + mark3
print("Total:", totalScore, "/350")

gradeScore = {284 <= totalScore <= 350:grades[0],
    246 <= totalScore < 284:grades[1],
    208 <= totalScore < 246:grades[2],
    171 <= totalScore  < 208:grades[3],
    134 <= totalScore < 171:grades[4],
    97 <= totalScore < 134:grades[5],
    0 <= totalScore < 97:grades[6]}

for i in gradeScore:
    print("Grade: ", gradeScore[i])

And then when i input the scores it adds it up correctly but prints:

Computer systems score /140: 57
Algorithms and programming score /140: 68
Programming project score /70: 35
Total: 160 /350
Grade:  U
Grade:  D

I also need to print the amount of marks above the grade boundary you're at and the amount of marks below the next grade, how would i do this without a long if statement?

Upvotes: 0

Views: 107

Answers (3)

Alex P
Alex P

Reputation: 1155

it just prints grade: U and then the correct grade as well and i don't know why this is.

First of all, the order the grades are printed is not always the same (i.e. first U and then the correct grade). It depends on what your input is. If you select inputs that land you in grade A*, then A* is printed first and U is printed second.

The reason this happens, is because you are creating a dictionary with just two keys (True and False), but you are setting their value multiple times. When you are in grade A*, the True key is added in the dictionary first, and that is why it is printed first. However, when you are in any other grade, the key False is added first, and that's why U is printed first.

But that's not the end. Grade U is always present because the last key you are trying to insert/modify (regardless of the key) gets the value grades[6], which is U.

I have no idea why you went with this approach, but let me say that it's not the correct one. A correct approach (but not the only one) is presented below:

grades = ["A*", "A", "B", "C", "D", "E", "U"]

mark1 = int(input("Computer systems score /140: "))
mark2 = int(input("Algorithms and programming score /140: "))
mark3 = int(input("Programming project score /70: "))
totalScore = mark1 + mark2 + mark3
print("Total:", totalScore, "/350")

minScoreForGrade = [284, 246, 208, 171, 134, 97, 0]

for i, s in enumerate(minScoreForGrade):
    if totalScore >= s:
        print("Grade:", grades[i])
        print("Marks above boundary:", totalScore-s)
        if i > 0:
            print("Marks for next grade:", minScoreForGrade[i-1]-totalScore)
        else:
            print("Marks for next grade: --")
        break

Explanation

You create a list of the minimum score necessary for each grade. You then run through the list, from the highest score to the lowest, to find the first time your totalScore is above a grade's baseline. Once you find that, you can print the information you want with simple arithmetic and you break out of the loop.

In your problem, a dictionary is not an appropriate data structure to use.

Note: Answers that suggest multiple if statements are simply wrong. They may get the result you need, but it's a terrible coding practice. It makes your code cluttered, very frustrating to maintain and most of all, prone to errors. Instead, always try to find pythonic ways, based on simple logic, to get results you want.

Upvotes: 2

Yassine Majdoub
Yassine Majdoub

Reputation: 154

I tried to work your assignement a little bit and I think this may help you:

First, You need to put a condition on your entries as you have a maximum and minimum mark:

mark1 = int(input("Computer systems score /140: "))
    while mark1 not in range(0,141):
    print("mark1 out of range!")
    mark1 = int(input("Computer systems score /140: "))
mark2 = int(input("Algorithms and programming score /140: "))
    while mark2 not in range(0,141):
    print("mark2 out of range!")
    mark2 = int(input("Computer systems score /140: "))
mark3 = int(input("Programming project score /70: "))
    while mark3 not in range(0,71):
    print("mark3 out of range!")
    mark3 = int(input("Computer systems score /70: "))

Now to get the vector ("Grade", "marks to upper limit", "marks to the lower limit"), I propose the solution below:

if 284 <= totalScore <= 350:
    print("Grade: ", grades[0],"\nMarks above: ", 350-totalScore,"\nMarks below: ", totalScore-284)
elif 246 <= totalScore < 284:
    print("Grade: ", grades[1],"\nMarks above: ", 284-totalScore,"\nMarks below: ", totalScore-246)
elif 208 <= totalScore < 246:
    print("Grade: ", grades[2],"\nMarks above: ", 246-totalScore,"\nMarks below: ", totalScore-208)
elif 171 <= totalScore  < 208:
    print("Grade: ", grades[3],"\nMarks above: ", 208-totalScore,"\nMarks below: ", totalScore-171)
elif 134 <= totalScore < 171:
    print("Grade: ", grades[4],"\nMarks above: ", 171-totalScore,"\nMarks below: ", totalScore-134)
elif 97 <= totalScore < 134:
    print("Grade: ", grades[5],"\nMarks above: ", 134-totalScore,"\nMarks below: ", totalScore-97)
elif 0 <= totalScore < 97:
    print("Grade: ", grades[6],"\nMarks above: ", 97-totalScore,"\nMarks below: ", totalScore)

Upvotes: 1

An economist
An economist

Reputation: 1311

Keys in dictionary have to be unique. In your current dictionary keys will be assigned based on the totalScore and therfore you will have multiple False and single True. As a result only the single pair of False and True keys are returned.

You can see that different scores yield different dictionaries:

Total: 150 /350
{False: 'U', True: 'D'} 

Total: 0 /350
{False: 'E', True: 'U'}

However, you can return the correct grade by printing only the True value from the dictionary. In your solution, looping over the dictionary will return all values from the dictionary (which always include both False and True keys). Just replace the for loop with simple print() statment:

print("Grade: ", gradeScore[True])

Alternatively, you can write function to return the correct grade:

def getGrade(totalScore):
    if 284 <= totalScore <= 350:
        return "A*"
    elif 246 <= totalScore < 284:
        return "A"
    elif 208 <= totalScore < 246:
        return "B"
    elif 171 <= totalScore < 208:
        return "C"
    elif 134 <= totalScore < 171:
        return "D"
    elif 97 <= totalScore < 134:
        return "E"
    elif 0 <= totalScore < 97:
        return "U"

And then print the grade:

print("Grade: ", getGrade(totalScore))

Upvotes: 1

Related Questions