Python

Moderators: None (Apply to moderate this forum)
Number of threads: 473
Number of posts: 1172

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Please correct this Posted by hammer on 11 Oct 2002 at 1:57 PM
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 "<stdin>", 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!!!
Report
Re: Please correct this Posted by infidel on 11 Oct 2002 at 3:18 PM
: 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 "<stdin>", 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:

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


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.
Report
Thanks a lot but... Posted by hammer on 12 Oct 2002 at 12:46 AM
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:
:
:
: 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])
: 

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

Report
Re: Thanks a lot but... Posted by infidel on 15 Oct 2002 at 1:56 PM
: 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

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




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.