2017-06-14 4 views
1

を区切る場所を判断して行を区切るしようとしています。後に大なり記号が続き、その後に大文字が見つかると、その行が新しい行の先頭であることがわかるので、そこには\nが置かれます。私は "ローカル変数"エラーに遭遇していますが、私は理由を知りません。関数内で変数を使用しており、グローバル変数を使用していないので、これは起こってはいけません。奇妙なことは、コードが '独立した'関数の最初の3つの呼び出しでは動作しますが、4番目の呼び出しでは機能しないということです。私が持っている唯一の説明は、while (example[x] != "<"):ループが、何らかの理由で、「別個の」関数の4番目の呼び出しで全く実行されていないということです。私はコードをテストするために行の関数を4回使用していだから基本的に私は、テキストファイルを見しようと記号未満を探したコードの一部を持っている特定の文字を見て、</p> <p>のPython 3を使用して

temp_file_string = example[:lineholder] 

:線上

local variable 'lineholder' referenced before assignment 

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

エラーメッセージがあると述べています。私は単純に関数をループすることができたいと思うし、 "例"を自動的に処理し、すべてのタグが記述された後に改行を置く。

出力は次のようになります。

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 

私はので、私は私のコードはかなり悪いと厄介である知っているPythonのにかなり新しいだと一般的にコーディング。私は真剣にここで間違っている何かがあることを知っているので、私を訂正してください。

答えて

0

最初の条件while (example[x] != "<")が偽であり、ループが実行されない場合、lineholder = xが実行されない場合に問題が発生します。

lineholder = xをどこか先に初期化する必要があります。

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): 
    lineholder = x 
    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)) 
関連する問題

 関連する問題