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

Recommended Answers

All 4 Replies

Member Avatar for masterofpuppets

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 masterofpuppets

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.