2014-04-19 1 views
0

저는 파이썬에서 일종의 암호화 도구로 작업 해 왔습니다. 이 코드는 해독 기능을위한 것입니다.파이썬 숫자로 단어 변환기가 공간 감지기가 필요합니다

요점은 주어진 숫자를 가져 와서 주어진 키로 나눌 위치의 목록에 삽입하는 것입니다.

코드에 대한 내 아이디어는 아래에 나와 있지만 시도 할 때마다 계속 out of list index range이됩니다. 어떤 제안? 저는 초보자입니다.

need = [] 
detr = raw_input('What would you like decrypted?') 
count = 0 
for d in detr: 
    if (d == '.' or d == '!') or (d.isalpha() or d== " "): 
     count +=1 
    else: 
     need[count].append(d) 

답변

0

존재하지 않는 목록 값을 덮어 쓰려고 시도하는 중입니다.

list.append(item)list의 끝에 item을 더합니다. list[index] = itemlistindex이라는 상품을 넣습니다.

list = [0,0,0] 
list.append(0) # = [0,0,0,0] 
list[0] = 1 # [1,0,0,0] 
list[99] = 1 # ERROR: out of list index range 

count 변수를 완전히 제거해야합니다. d==' ' 등의 경우 None을 추가하거나 무시할 수 있습니다.

0

귀하의 설명을 이해하는 방식으로 문자열의 숫자를 추출하여 for 루프를 사용하여 각 문자를 반복하는 목록에 추가하려고합니다.

정규식 (r'([\d]+)'과 같은)을 사용하면 더 쉬울 것이라고 생각합니다. 는 그러나 방법 joconner는 말했다 :

need = [] 
detr = input('What would you like decrypted?\n') 
i = iter(detr) # get an iterator 
# iterate over the input-string 
for d in i: 
    numberstr = "" 
    try: 
     # as long as there are digits 
     while d.isdigit(): 
      # append them to a cache-string 
      numberstr+= d 
      d = next(i) 
    except StopIteration: 
      # occurs when there are no more characters in detr 
      pass 
    if numberstr != "": 
     # convert the cache-string to an int 
     # and append the int to the need-array 
     need.append(int(numberstr)) 

# print the need-array to see what is inside 
print(need) 
: "카운트 변수를 제거하기"