NameError Problem

Tim Peters tim_one at email.msn.com
Sat Jun 19 02:33:17 CEST 1999


[Jonathon]
> 	I'm writing a long << 25K line >> script to verify data from a
>  	database.
>
> 	The following bit of script is giving me a NameError, and
> 	I can't figure out why.
>
> 	datafile = open ( the_data_file_is, "r" )
> 	new_datafile = open ( the_temp_data_file_is, "w" )
> 	line_kounter = 0
> 	while line_kounter < 50000
> 	    line_kounter = line_kounter + 1
> 	    templine = datafile.readline(line_kounter)
>             validate_line = templine
> 	    if ( the_file_is == "john_www.db" ) :
> 	         temp_record_id = int(validate_line[2:18])
> 		 temp_country  = strip(validate_line[20:50])
>             elif ( the_file_is == "jan_www.db " ):
>                  temp_record_id = int(validate_line[2:10])
>                  temp_country = strip(validate_line[12:20])
>             print temp_record_id
>
> 	At that point I get a NameError message.
> 	All I'm trying to do is have it print to screen
> 	the record number it is working on.
>
> 	I've double checked the white spaces, and they are
> 	all lined up at 0, 4, 8, 12, 16 etc.
>
> 	What am I missing here?  << And how do I correct it. >>
> ...

1) Don't mix tabs and spaces.

2) Use "python -t" or tabnanny.py (not your eyes) to check for consistent
indentation if you can't live with #1.

3) Post the code you're actually running (e.g., this version is missing a
":" after the "while" stmt, so Python won't run it at all).

4) After the "new_datafile = ..." line, insert

        temp_record_id = "Oops!  I never gave this a value!!  Silly me."

and then see what your program prints <wink>.

if/elif-without-an-else-ly y'rs  - tim






More information about the Python-list mailing list