Hello EVeryone...

I really need some help with this problem...
A certain CS professor gives 100 - point quiz graded on scale
90 - 100:A
80 - 89:B
70- 79: C ,
60 - 60:D
<60 :F

I need to write a program that eccepts an exam score as input and prints the corresponding grade...


First question i have is should i use Lists to present the numbers in the code? or is there another way... what would be the best....

Thanx...

Dani AI

Generated

A compact, robust alternative to the per-score lists and long if/elif chains already shown: validate the input, keep a small sorted list of cutoff thresholds, and use Python’s bisect to find the right bucket. This avoids building giant arrays of individual scores (as in ’s first attempt) and is easier to extend than a big cascade of conditionals (as discussed by and ). ’s suggestion to think in ranges is the right idea — bisect implements that cleanly.

import bisect

cutoffs = [60, 70, 80, 90]          # lower bounds for D, C, B, A
letters = ['F', 'D', 'C', 'B', 'A'] # index from bisect result

def grade(score):
    s = float(score)
    if not (0 <= s <= 100):
        raise ValueError("score must be between 0 and 100")
    i = bisect.bisect_right(cutoffs, s)
    return letters[i]

Explanation: bisect_right returns the insertion index for s into cutoffs (so 59 -> 0, 60 -> 1, 90 -> 4). Using that index on letters yields the correct letter. The function accepts decimals (e.g., 89.5 -> 'B') and enforces a 0–100 range.

Troubleshooting and variations: add a try/except around float() to produce user-friendly errors; use a list comprehension or map() to convert many scores; change cutoffs to finer-grained values to implement plus/minus grading; if thresholds become irregular, keep them as (threshold, letter) pairs sorted by threshold and use bisect on the thresholds. For learning purposes, simple if/elif chains are fine; this approach is the next step toward concise, maintainable code.

Recommended Answers

All 7 Replies

Suppose that you were the computer; how would you solve this problem? For example, I give you an 86. How do you know to return a 'B'?

Write a short plan that a human could follow; then turn it into a Python program.

Post your plan here, if you like, and we can help refine it.

Jeff

Here is what i've got...

def main():
    
    letter(A) = ["100", "99", "98", "97",
              "96", "95", "94", "93", "92", "91", "90"]
    letter2(B) = ["89", "88", "87",
              "86", "85", "84", "83", "82", "81", "80"]
    #and so on... but afeter this i have no idea how to make computer look at the
    #right list and print the corresponding grade...



    
    score = input("Enter quiz score: ")
    
    
                             #over here... how do i make computer look at letter

    print "Letter grade is", letter[score-1] + "."
    
main()

please somebody answer...

OK, so that's code...but it's not a plan. The plan is higher-level and leaves the details for later.

Reading between the lines, your plan seems to be something like

get the numeric score
look up the score in a list   # < This is the part you're trying to solve.
print result

The way that you are trying to look up the grade in the list is what dictionaries are made for, so when you get there in your course, think of this problem.

For now, you need something different.

Here's a thought: suppose you had

grades = ["F","D","C","B","A"]

so that grades[0] = "F", etc.

Can you come up with a way to turn a number from 0 - 100 into the right number from 0 - 4 so that you could look up the number on the list?

OR

Could you use a series of if-elif statements to figure out the right letter to print?

Jeff

Hey guy this is auctualy the answer...

you've got to write the code like this...


...
grades = 60 * F + 10 * d + 10 * c + 10 * B + 11 * A

this will give you 60 Fs and 10 Ds and so on...

just have it... maybe it will help somebody...

Look at your conditions:
90 - 100: A
80 - 89: B
70 - 79: C
60 - 69: D
< 60: F

The last line says, if the score is less than 60, then the grade is an F. That would be the easiest to write for the computer to understand ...

if score < 60:
    grade = 'F'

If your score is higher than or equal to 90, the grade will be an A ...

if score >= 90:
    grade = 'A'

Now the logic expression for grade B is more complex. If the score is greater than or equal to 80 and the score is less than or equal to 89, then the grade is a B. For the computer ...

if score >= 80 and score <= 89:
    grade = 'B'

The conditions are similar for grades C and D.

Once you have all your if statements written, assign score a value (0 to 100), send it through the ifs, and at the end print your resulting grade to test it all out.

You can also use:

if score < 60:
     grade="F"
elif score < 70:
     grade="D"
elif score < 80:
     grade="C"
elif score < 90:
     grade="B"
elif score < 101:
     grade="A"
else:
     print score, "is bogus"

but as stated earlier, it depends on what makes sense to you. i.e. what the outline is. Also, do you want to keep track of multiple scores for mulitple people. If so, that adds another dimension to the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.