2011-08-04 4 views
0
import string 

## This part of the code initializes the program by recieving inputs and saving 
## them as strings. 
## It also currently works! 

intext = str.lower(raw_input("Please input the text you are going to use. ")) 
##print intext 
instring = str.lower(raw_input("Please input your string. ")) 
##print instring 
inlengthminus = int(raw_input("Please input the number of words you would like before the matched word. ONLY PUT AN INTEGER HERE!")) 
inlengthplus = int(raw_input("Please input the number of words you would like after the matched word. ONLY PUT AN INTEGER HERE!")) 
## This part of the code splits the text into searchable lists. 
## It only works when the cases match, which is why the search lists 
## convert the text to lowercase. 

searchtext=str.split(intext) 
##print searchtext 
searchstring=list(instring+1) 
##print searchstring 

## This is the actual search process. 
## It works, mostly. 

length = len(searchtext) 
stringlength = len(searchstring) 
##print stringlength 

x=0 
for a in range(0,length): 
    print a 
    print searchstring[x] 
    print x 
    if searchstring[x] in searchtext[a]: 
     if (a-inlengthminus)<0: 
      print "You almost caused an error, please pick a number of words before your matched word that won't call text that doesn't exist." 
      break 
     else: 
      print searchtext[a-inlengthminus:a+inlengthplus+1]  
      x+=1 
    else: 
     pass 

이 프로그램이 searchstring [x]의 값을 searchstring보다 길게 호출하는 것을 중단하는 법을 모르겠습니다. x가이 오버플로 오류를 일으키는 것을 막을 수있는 방법이 있습니까?이 프로그램이 존재하지 않는 목록 요소를 호출하는 것을 어떻게 중지합니까?

+1

'x '가'searchstring'의 길이를 지났을 때 당신은 무엇을하고 싶습니까? – GaretJax

+0

이 코드는 무엇을하고 있습니까? 어떤 종류의 입출력을 찾고 있습니까? – Santa

+1

메시지 생성기입니다. 이 프로그램은 입력 된 텍스트 (예 : 소설 또는 뉴스 보고서)와 입력 문자열 (예 : John Cage)을 가져 와서 입력 문자열에 문자가 들어있는 텍스트를 가져옵니다. 입력 문자열의 마지막 문자를 지나갈 때 x를 0으로 재설정하여 텍스트의 길이가 지나갈 때까지 프로그램이 계속 실행되도록합니다. 제 질문을 명확히하도록 도와 주셔서 감사합니다. – Alex

답변

1

x+=1 

교체 모듈러스 연산자 부문을 수행하고 나머지를 반환

x = (x + 1) % len(searchstring) 

으로. x가 0에서부터 searchstring의 길이까지 x는 아무 것도하지 않습니다 (자체 반환). x == searchstring의 길이 인 경우, 나눌 때 나머지가 없기 때문에 "resets"를 0으로 설정합니다.

+0

그랬어! 초보자를 도와 주셔서 감사합니다. – Alex

+0

@alex 정규 표현식과're' 모듈을 사용해보십시오. – Keith

관련 문제