how could i call the above function so that when i input something like 21 for the mark the ouput is "the grade is A", and when the user inputs something more than 25 and less than 0 hes asked to input it again..?

def mark2Grade(mark):
    if mark >=20:
        return "A"
    elif mark >=15:
        return "B"
    elif mark >=10:
        return "C"
    else:
        return "Fail"

def gradeTest():
    mark=input("please enter the mark: ")
    print "your grade is", mark2Grade

Dani AI

Generated

Thread recap and practical fix tied to the existing suggestions from and : the function that converts a numeric mark to a grade should be called with the numeric input, the input must be validated (0..25) and non-numeric input must be handled, and it is safer to re-prompt with a loop instead of recursion. The snippet below shows a compact, more robust pattern that keeps the grade logic separate, validates input safely across Python 2/3, and uses a loop for re-prompting.

# Python2/3 safe input selection
try:
    read = raw_input
except NameError:
    read = input

GRADE_THRESHOLDS = [(20, 'A'), (15, 'B'), (10, 'C'), (0, 'Fail')]

def grade_from_score(score):
    for thr, g in GRADE_THRESHOLDS:
        if score >= thr:
            return g
    return 'Fail'

def get_valid_score(min_v=0, max_v=25, prompt='Enter mark (0-25): '):
    while True:
        s = read(prompt)
        try:
            n = int(s)
        except ValueError:
            print('Please enter an integer.')
            continue
        if not (min_v <= n <= max_v):
            print('Out of range; try again.')
            continue
        return n

if __name__ == '__main__':
    score = get_valid_score()
    print('The grade is', grade_from_score(score))

Notes and troubleshooting:

  • The compatibility block (assigning read) avoids the unsafe behavior of input() in Python 2 and works cleanly in Python 3.
  • Using a thresholds list keeps the grade logic maintainable; adding or changing bands requires only data changes.
  • A while loop prevents deep recursion that could happen with repeated bad input, and gives clearer control for things like maximum retry limits.
  • For non-integer marks (floats) change int(s) to float(s) and adjust thresholds as needed.
  • If a script must run unattended, return an error code or raise an exception instead of an interactive loop.

This approach builds on ’s range-check idea while addressing input parsing and long-term maintainability.

Recommended Answers

All 4 Replies

Member Avatar for Member #562630

you almost have it, all you have to do is to call the function with mark as an argument like this:

def mark2Grade( m ):
    if m >= 20:
        return "A"
    elif m >= 15:
        return "B"
    elif m >= 10:
        return "C"
    else:
        return "Fail"
 
def gradeTest():
    mark = input( "Please enter the mark: " )
    print "Your grade is", mark2Grade( mark )

gradeTest()

See if you can figure out how to implement the check for validity, i.e. < 0 and > 25 :)

you almost have it, all you have to do is to call the function with mark as an argument like this:

def mark2Grade( m ):
    if m >= 20:
        return "A"
    elif m >= 15:
        return "B"
    elif m >= 10:
        return "C"
    else:
        return "Fail"
 
def gradeTest():
    mark = input( "Please enter the mark: " )
    print "Your grade is", mark2Grade( mark )

gradeTest()

See if you can figure out how to implement the check for validity, i.e. < 0 and > 25 :)

anything on these lines?

def gradeTest():
    mark = input( "Please enter the mark: " )
    if mark >25:
        print "Your grade is", mark2Grade( mark )
        else:
            print"try again"
            gradeTest()

gradeTest()
Member Avatar for Member #562630

hi,

def gradeTest():
    mark = input( "Please enter the mark: " )
    if mark >25:
        print "Your grade is", mark2Grade( mark )
        else:
            print"try again"
            gradeTest()
 
gradeTest()

be careful with the indentation for the else statement here :)

Now, this means that the result will be shown only if the mark is greater than 25, which is not helpful. You need the mark to be in range 0 <= mark <= 25 right? so the if would be:

def gradeTest():
    mark = input( "Please enter the mark: " )
    if 0 > mark or mark > 25:
        print "Try again!"
        gradeTest()
    else:
        print "Your grade is", mark2Grade( mark )
 
gradeTest()

hi,

be careful with the indentation for the else statement here :)

Now, this means that the result will be shown only if the mark is greater than 25, which is not helpful. You need the mark to be in range 0 <= mark <= 25 right? so the if would be:

def gradeTest():
    mark = input( "Please enter the mark: " )
    if 0 > mark or mark > 25:
        print "Try again!"
        gradeTest()
    else:
        print "Your grade is", mark2Grade( mark )
 
gradeTest()

yeah i just wasn't sure about how you do the second bit which is less than 25 and more than 0.... i was using the "and" operation whcih did not work.... thx for the help, you are very helpful..

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.