Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Please correct this

hammerhammer Posts: 3Member
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

  • infidelinfidel Posts: 2,900Member
    : 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.
  • hammerhammer Posts: 3Member
    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.
    :

  • infidelinfidel Posts: 2,900Member
    : 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.