Using Python 3.
So basically I have a piece of code that tries to look at a text file and looks for a less than sign. If it finds a greater than sign after it, and then an uppercase letter afterwards, it knows that it is the beginning of a new line so it places a \n
there. I'm running into a "local variable" error but I have no idea why. It shouldn't be happening since I'm using the variable inside a function and not using any global variables. The weird thing is, is that the code works for the first three calls of the 'separate' function, but not on the fourth. The only explanation I have is that the while (example[x] != "<"):
loop is not executing at all on the fourth call of the 'separate' function for whatever reason.
example = "Tags for HTML: <!--...--> Defines a comment <!DOCTYPE> Defines the document
type <a> Defines a hyperlink <abbr> Defines an abbreviation or an acronym <acronym> Not
supported in HTML5. Use <abbr> instead. Defines an acronym <address> Defines contact
information for the author/owner of a document <applet> Not supported in HTML5. Use
<embed> or <object> instead. Defines an embedded applet <area> Defines an area inside
an image-map <article> Defines an article <aside> Defines content aside from the page
content <audio> Defines sound content <b> Defines bold text"
x = 0
def separate(x, example):
f=open('format_output.txt','w')
#looking for the first less than sign
while (example[x] != "<"):
x+=1
#advancing and storing the lineholder so it can enter a newline when done
lineholder = x
#looking for the first greater than sign
while (example[x] != ">"):
#advancing the cursor
x+=1
#checking if the position two characters from the cursor is an uppercase letter
this = example[x+2:x+3]
if(this.isupper()):
#if it is, print "it's upper"
print("its upper")
#putting everything before lineholder into a variable
temp_file_string = example[:lineholder]
#adding a newline to it
temp_file_string = temp_file_string + "\r\n"
#putting everything after linholder into another variable
rest_of_string = example[lineholder:]
#writing them combined into the output file
f.write(temp_file_string + rest_of_string)
#rewinding the file cursor so the file can be read and printed to the shell
f.seek(0)
f=open('format_output.txt','r')
example = f.read()
print("\n\nprinting contents:\n\n" + example)
f.close
return (x, example)
else:
#else say 'Isn't Uppper'
lineholder = x
print("Isn't Upper")
return (x , example)
(x, example) = separate(x, example)
(x, example) = separate(x, example)
(x, example) = separate(x, example)
(x, example) = separate(x, example)
print('\n'+str(x))
The error message states that:
local variable 'lineholder' referenced before assignment
on line:
temp_file_string = example[:lineholder]
I'm using the function four times in a row just to test the code. I want to be able to simply loop the function and it handles "example" automatically, placing a newline after every tag is done being described.
The output should be:
Tags for HTML:
<!--...--> Defines a comment
<!DOCTYPE> Defines the document type
<a> Defines a hyperlink
<abbr> Defines an abbreviation or an acronym
<acronym> Not supported in HTML5. Use <abbr> instead. Defines an acronym
<address> Defines contact information for the author/owner of a document
<applet> Not supported in HTML5. Use <embed> or <object> instead. Defines an embedded applet
<area> Defines an area inside an image-map
<article> Defines an article
<aside> Defines content aside from the page content
<audio> Defines sound content
<b> Defines bold text
I'm fairly new to Python and coding in general so I know my code is pretty bad and messy. I know there's something I'm doing seriously wrong here so correct me please.