2011-12-15 4 views
-3

파이썬 2.7x를 사용하고 있으며 디버깅하는 데 문제가 있습니다. 내가 뭘 할 수 있는지 잘 모르겠다. 어떤 도움을 주셔서 감사합니다. 감사.파이썬 코드를 디버깅하는 데 도움이 필요합니다. 난파선입니다.

import re 
import sys 

# Grab parameters from the command line 
(filename, threshold) = sys.argv[1:3] 

# Validate arguments 
if (re.match("\D", threshold)): 
    print "The threshold must be a number." 
    sys.exit(1) 

# Read file and tally word frequencies 
fh = open(filename) 
file = fh.read() 
words = [] 
for line in file.split('\n'): 
    found = 0 
    for word in words: 
     if word[0] == line.lower(): 
     found = 1 
     word[1] += 1 

    # initialize a new word with a frequency of 1 
    if found == 0: 
     words.append([line, 1]) 

# Print words and their frequencies, sorted alphabetically by word. Only print a word if its frequency is greater than or equal to the threshold. 
for word in sorted(words): 
    if word[0] < threshold: continue 
    print "%4d %s" % (word[1], word[0]) 
+1

작동하지 않는 것에 대한 정보를 입력해야합니다. –

+0

"디버깅하는 데 문제가 있습니다." 디버거와 관련된 특정 문제가 있습니까? 작동하지 않는 것은 무엇입니까? – David

+0

이것이 닫혀 있어야한다고 생각하지 마십시오. 질문이 잘못 제기되었지만 유효하지 않습니다. @ user1100031, 내가 추측해야만한다면, 단어 목록에 아무 것도 추가하지 않는다는 것이 문제라고 말할 수 있습니다. 빈 상태로 시작하기 때문에'for word in words '루프를 입력하지 마십시오. –

답변

2

일반적으로 pdb 모듈을 사용하면 파이썬 코드를 디버깅하는 것이 가장 쉽습니다. 디버거를 시작하려면 다음 코드를 넣습니다 :

import pdb 
pdb.set_trace() 

당신은 값을 출력하는 p을 함수로 단계로, s을 코드의 다음 줄을 실행하기 위해 n를 사용 할 수 있습니다 (예를 들어, p words 인쇄됩니다 당신의 단어 목록).

문제에 대한 추가 정보없이 코드의 상황을 알 수는 없지만 일관성없는 사례와 관련된 문제가있는 것으로 보입니다. 단어 목록에 항목을 추가 할 때는 소문자로 입력해야합니다.

if found == 0: 
    words.append([line.lower(), 1]) 

또한 문자열을 임계 값과 비교합니다. 숫자가 아닙니다. 다음과 같아야합니다.

if word[1] < threshold: continue 

이 정보가 도움이되기를 바랍니다.

관련 문제