2013-05-14 2 views
-5

변수에 특정 행 번호를 할당 한 다음 전역 변수에 할당 한 다음 인쇄합니다.이 코드의 잘못된 점은 무엇입니까?

bookStartLine = None 
bookEndLine = None 


def grabLine(currentUrl): 

    ### blah blah defines what lines is 
    index=0 
    for line in lines: 
     index+=1 
     if "*** START OF THE PROJECT" in line: 
      currentBookStartLine = index 
     if "*** END OF THE PROJECT" in line: 
      currentBookEndLine = index 


    global bookStartLine 
    global bookEndLine 

    bookStartLine = currentBookStartLine 
    bookEndLine = currentBookEndLine 


grabline('http://www.gutenberg.org/cache/epub/768/pg768.txt') 

print(bookStartLine) 
print(bookEndLine) 
+0

? "이 코드에 어떤 문제" - 디버깅을 시도하십시오 ... –

+0

어떻게해야할지 모르겠습니다. 나는 이것에 아주 새로운 것이다. 어떤 충고?? – Deivore

+0

초심자는 보통 print 문으로 시작합니다. print()를 사용하여 다양한 변수 값을 표시하고 문제를 찾아 낼 수 있습니다. –

답변

0

이 코드는 stonking하고 엉망

global bookStartLine 
global bookEndLine 


def grabLine(currentUrl): 
    ### blah blah defines what lines is 
    currentBookStartLine,currentBookEndLine = False,False #needed to define these before use below 
    for index,line in enumerate(lines,start=1): #start = 1 so index gets 1 on first iteration 
     if "*** START OF THE PROJECT" in line: 
      currentBookStartLine = index 
     if "*** END OF THE PROJECT" in line: 
      currentBookEndLine = index 

    bookStartLine = currentBookStartLine 
    bookEndLine = currentBookEndLine 


grabLine('http://www.gutenberg.org/cache/epub/768/pg768.txt') #grabLine not grabline 

print(bookStartLine) 
print(bookEndLine) 
+0

감사합니다. 하지만 코드에 변경을 가하면 두 변수에 대해 "거짓"을 인쇄합니다 ... – Deivore

+0

@Deivore 죄송합니다.'[ 's', s ']'를'lines'로 바꾸는 것을 잊었습니다. 정의 된 행이 없기 때문에 테스트에 추가했습니다. – HennyH

+0

그 날 준 : TypeError : 유형 str 버퍼 API를 지원하지 않습니다 – Deivore

0

을 시도해보십시오

import urllib.request 

bookName  = None 
authorName = None 
bookStartLine = None 
bookEndLine = None 


def parser(currentUrl): #parses texts to extract their title, author, begginning line and end line 
    global bookName 
    global authorName 
    global bookStartLine 
    global bookEndLine 
    global url 

    url = 'http://www.gutenberg.org/cache/epub/1232/pg1232.txt' #machiaveli 
    url = currentUrl  
    book = urllib.request.urlopen(url) 
    lines = book.readlines() 
    book.close() 

    finalLines = [line.decode()[:-2] for line in lines] 


    for line in finalLines: 
     if "Title" in line: 
      currentBookName = line[7:len(line)] 
      break 
    for line in finalLines: 
     if "Author" in line: 
      currentAuthorName = line[8:len(line)] 
      break 
    currentBookStartLine,currentBookEndLine = False,False 

    for index,line in enumerate(line,start=1): 
     if "*** START OF THE PROJECT" in line: 
      currentBookStartLine = index 

     if "*** END OF THE PROJECT" in line: 
      currentBookEndLine = index 



    url   = currentUrl 
    bookName  = currentBookName 
    authorName = currentAuthorName 
    bookStartLine = currentBookStartLine 
    bookEndLine = currentBookEndLine 


parser('http://www.gutenberg.org/cache/epub/768/pg768.txt') #wuthering heights 
print(url) 
print(bookName) 
print(authorName) 
print(bookStartLine) 
print(bookEndLine) 
관련 문제