J!NX Forums
J!NX Forums
Home | Forum Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
This username differs from J!NX login...
Save Password
Forgot your Password?

 All Forums
 TechSpeak
 Code Poets
 Python program
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

piyrwq
Alpha Member

USA
11 Posts

Posted - 11/02/2009 :  3:50:42 PM  Show Profile  Reply with Quote

This is trying to read stuff from a file, calculate stuff, then display everything in a tabbed format. It say the name 'resident_students' is not defined.

I think I also need to convert number_of_credits to an interger but I am not sure where to do that.







residential_rate=78
nonresidential_rate=139

def main():
    total_tuition=0
    resident_students=0
    nonresident_students=0
    
    
    
    try:
        
        print 'student name\t resident code\t credits\t tuition due'
        print
    
        infile=open(tuition.txt,'r')
    
        student_name=infile.readline()
    
        while student_name != "":
            
            student_name=student_name.rstrip('\n')
            print student_name + '\t'
            
            resident_code=resident_code.rstrip('\n')
            print resident_code + '\t'
            
            credits=credits.rstrip('\n')
            print number_of_credits + '\t'
            
            if resident_code == 'R' or resident_code == 'r':
                tuition_due = credits * residential_rate
                resident_students += 1
                
                
                
            elif resident_code=='N' or resident_code=='n':
                tuition_due=credits * nonresidential_rate
                nonresident_students += 1
                                
                
        else:
            tuition_due=0
                
        total_tuition += tuition_due
    
        if tuition_due==0:
            print '\t' + 'invalid code'
                
        else:
            print '\t'+'$%.2f' % tuition_due


    except IOError:
        print'an error occured trying to open the file'

    infile.close()
            
            
print
print'total number of residential students', resident_students
print'total number of residential students', nonresident_students
print'total tuition due', '$%.2f' % total_tuition
            
            
main()

int
Alpha Member

40 Posts

Posted - 11/02/2009 :  4:27:35 PM  Show Profile  Reply with Quote
Those four print lines near the bottom aren't part of main (they aren't tabbed over) but your variable is being defined inside of main

Kid. Do not fuck with me on this issue. Hardware is my domain, and you had better tread softly.
Go to Top of Page

hexed
Advanced Member

United Kingdom
2651 Posts

Posted - 11/02/2009 :  4:31:08 PM  Show Profile  Reply with Quote
You define 'resident_students' in main, but then use it outside of main - it's only valid within the scope you defined it in (aka, if you declare it in a function, it's only valid in that function). So, just move the total_tuition/resident_students/nonresident_students definitions to outside of main.

quote:
I think I also need to convert number_of_credits to an interger but I am not sure where to do that.

number = int(string)


Edit - damn you int, I'll get you next time.

Edited by - hexed on 11/02/2009 4:31:30 PM
Go to Top of Page

piyrwq
Alpha Member

USA
11 Posts

Posted - 11/02/2009 :  4:46:51 PM  Show Profile  Reply with Quote
So I could do it this way?

number_of_credits=credits.rstrip('\n')
            credits=int(number_of_credits)
            print number_of_credits + '\t'



Now it is printing out the heading and saying "global name 'tuition' is not defined".

I'm pretty sure that means it isn't finding the tuition file to read the stuff. The teacher said the file and the program both have to be in the same place to work but I'm using wingIDE, how can that be in the same place as a text file?
Go to Top of Page

hexed
Advanced Member

United Kingdom
2651 Posts

Posted - 11/02/2009 :  5:43:05 PM  Show Profile  Reply with Quote
quote:
Originally posted by piyrwq

So I could do it this way?
[...]

Yep

quote:
Now it is printing out the heading and saying "global name 'tuition' is not defined".

infile=open(tuition.txt,'r')

That tuition.txt should be in quotation marks as it's a string - it's not getting as far as trying to open it.

Edited by - hexed on 11/02/2009 5:44:05 PM
Go to Top of Page

piyrwq
Alpha Member

USA
11 Posts

Posted - 11/02/2009 :  5:56:27 PM  Show Profile  Reply with Quote
Ah ha. So now it says "local variable 'infile' referenced before assignment".
Go to Top of Page

hexed
Advanced Member

United Kingdom
2651 Posts

Posted - 11/02/2009 :  7:00:14 PM  Show Profile  Reply with Quote
quote:
Originally posted by piyrwq

Ah ha. So now it says "local variable 'infile' referenced before assignment".


Interesting. Does it say which line's doing that? I can't see where that is.

Possibly, it's because you're assigning infile inside the try...catch block, but then calling infile.close outside of that. So if open throws an exception, "infile" won't be assigned, but then at the end of main it calls infile.close without infile having been assigned to.

To solve this, put "infile = None" before the try block, then replace the infile.close call with something like

if infile != None:
    infile.close()

That way you only ever close infile if open has succeeded and set the infile variable.
Go to Top of Page

piyrwq
Alpha Member

USA
11 Posts

Posted - 11/02/2009 :  8:23:34 PM  Show Profile  Reply with Quote
Ah ha. The close infile was suppost to be inside, thanks. Now the rest of it works but it still can't open the file.
Go to Top of Page

neophytezer0
Omega Member

590 Posts

Posted - 11/03/2009 :  02:14:56 AM  Show Profile  Send neophytezer0 an AOL message  Click to see neophytezer0's MSN Messenger address  Send neophytezer0 a Yahoo! Message  Reply with Quote
make sure its in the same directory, or put the complete path.

Quote of the Month:
I must create a system, or be enslaved by another man's.
William Blake (1757 - 1827)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
J!NX Forums © J!NX and Snitz Go To Top Of Page
Snitz Forums 2000