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

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 :)

Edited 7 Years Ago by masterofpuppets: n/a

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()

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()

Edited 7 Years Ago by masterofpuppets: n/a

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..

This article has been dead for over six months. Start a new discussion instead.