Please correct this

Hey!! I need to write a program that gives lettergrades to data from a list. I have written this but when i import it, i have trouble. It says:
Traceback (most recent call last):
File "", line 1, in ?
File "grades.py", line 5
Print "A"

Grades.py is:
def Grade(L):
G=L[:]
for i in L:
if i >= 90 and i <= 100:
Print "A"
elif i>= 80 and i < 90:
Print "B"
elif i >= 70 and i < 80:
Print "C"
elif i >= 60 and i< 70:
Print "D"
elif i < 60:
Print "F"

Thank you,Please Help!!!

Comments

  • : Hey!! I need to write a program that gives lettergrades to data from a list. I have written this but when i import it, i have trouble. It says:
    : Traceback (most recent call last):
    : File "", line 1, in ?
    : File "grades.py", line 5
    : Print "A"
    :
    : Grades.py is:
    : def Grade(L):
    : G=L[:]
    : for i in L:
    : if i >= 90 and i <= 100:
    : Print "A"
    : elif i>= 80 and i < 90:
    : Print "B"
    : elif i >= 70 and i < 80:
    : Print "C"
    : elif i >= 60 and i< 70:
    : Print "D"
    : elif i < 60:
    : Print "F"
    :
    : Thank you,Please Help!!!


    Here is my version of your code:

    [code]
    def Grade(L):
    for i in L:
    if i >= 90:
    print "A"
    elif i >= 80:
    print "B"
    elif i >= 70:
    print "C"
    elif i >= 60:
    print "D"
    else:
    print "F"

    if __name__ == '__main__':
    Grade([99,88,77,66,55])
    [/code]

    Notice that "print" (lowercase p) is different than "Print" (uppercase P). Also notice that there's no reason to make a copy of your list. Also notice that you can really simplify your conditional expressions. There's no reason to check that i < 90 since that line won't execute unless it fails the first test. Also note that the "if __name__" test at the bottom of a script will execute if you are running your script by itself, but not if you import it into another script. It's a handy way of adding "self-test" code to your modules.
  • Hello!!
    Thank you very much, infidel. I rally appreciate that. I have defined another function because i want the out put to also be in a list and not just print. For example:
    If the input is lettergrade([98,76,84,62,12])
    Then i want the output to be [A,C,B,D,F]
    is there a way i can do this. I am sure there is but i am not really able to figure it out by myself. I gave it a try by making a copy but am really stuck there and am not able to move any further. The correction you have made works perfectly well and thank you once again. It would really help me if the output came out to be a list. Please help me if you can..



    Here is my version of your code:
    :
    : [code]
    : def Grade(L):
    : for i in L:
    : if i >= 90:
    : print "A"
    : elif i >= 80:
    : print "B"
    : elif i >= 70:
    : print "C"
    : elif i >= 60:
    : print "D"
    : else:
    : print "F"
    :
    : if __name__ == '__main__':
    : Grade([99,88,77,66,55])
    : [/code]
    :
    : Notice that "print" (lowercase p) is different than "Print" (uppercase P). Also notice that there's no reason to make a copy of your list. Also notice that you can really simplify your conditional expressions. There's no reason to check that i < 90 since that line won't execute unless it fails the first test. Also note that the "if __name__" test at the bottom of a script will execute if you are running your script by itself, but not if you import it into another script. It's a handy way of adding "self-test" code to your modules.
    :

  • : Hello!!
    : Thank you very much, infidel. I rally appreciate that. I have defined another function because i want the out put to also be in a list and not just print. For example:
    : If the input is lettergrade([98,76,84,62,12])
    : Then i want the output to be [A,C,B,D,F]

    This is so easy if you just stop and think about it for two seconds

    [code]
    def Grade(L):
    G = []
    newgrade = ""
    for i in L:
    if i >= 90:
    newgrade = "A"
    elif i>= 80:
    newgrade = "B"
    elif i >= 70:
    newgrade = "C"
    elif i >= 60:
    newgrade = "D"
    else:
    newgrade = "F"
    G.append(newgrade)
    return G

    if __name__ == '__main__':
    print Grade([99,77,66,88,55])
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories