| Author |
Topic  |
|
|
piyrwq
Alpha Member

USA
11 Posts |
Posted - 11/02/2009 : 3:50:42 PM
|
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
|
| 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. |
 |
|
|
hexed
Advanced Member
    
United Kingdom
2651 Posts |
Posted - 11/02/2009 : 4:31:08 PM
|
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 |
 |
|
|
piyrwq
Alpha Member

USA
11 Posts |
Posted - 11/02/2009 : 4:46:51 PM
|
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? |
 |
|
|
hexed
Advanced Member
    
United Kingdom
2651 Posts |
Posted - 11/02/2009 : 5:43:05 PM
|
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 |
 |
|
|
piyrwq
Alpha Member

USA
11 Posts |
Posted - 11/02/2009 : 5:56:27 PM
|
| Ah ha. So now it says "local variable 'infile' referenced before assignment". |
 |
|
|
hexed
Advanced Member
    
United Kingdom
2651 Posts |
Posted - 11/02/2009 : 7:00:14 PM
|
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. |
 |
|
|
piyrwq
Alpha Member

USA
11 Posts |
Posted - 11/02/2009 : 8:23:34 PM
|
| 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. |
 |
|
|
neophytezer0
Omega Member
  
590 Posts |
Posted - 11/03/2009 : 02:14:56 AM
|
| 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)
|
 |
|
| |
Topic  |
|