2014-10-22 3 views
-2

실수로 IDLE 2.7에이 프로그램을 작성했습니다 (초보자입니다). 이제 3.4에서 실행하려고합니다. 오류가 발생했습니다. 변경 사항을 적용했지만 아직 실행할 수 없습니다. 어떤 도움? 예 코드가 좋지 않을 수도 있지만 아직 작업 중입니다. 그래서 어떤 도움을 주시면 감사하겠습니다. 저에게는 두 업데이트의 주된 차이점은 괄호 만 있다고 생각했습니다.2.7에서 3.4

# Convert a decimal to a hex as a string 
def decimalToHex(decimalValue): 
    hex = "" 

    while decimalValue != 0: 
     hexValue = int(decimalValue) % 16 
     hex = toHexChar(hexValue) + hex 
     decimalValue = int(decimalValue) // 16  
    return hex 

def printRect (row_count, col_count): 
    row = [] 
    column = []  
    for r in range(row_count):   
     row = [] 
     column = [] 
     end_row_flag = 'False'   
     for c in range(col_count): 
      if r % (row_count) == 0: 
       if c % (col_count-1) == 0: 
        row.append('+') 
       else: 
        row.append('-')       
        end_row_flag = 'True'   
      if end_row_flag == 'True': 
       end_row = row   
      if c % (col_count-1) == 0: 
       column.append('|')    
      else: 
       column.append(' ')     
     if row: 
      print (row)   
     print (column)     
    print (end_row)  

def charASCII(letter):  
    return (ord(letter)) 

# Convert an integer to a single hex digit in a character 
def toHexChar(hexValue): 
    if 0 <= hexValue <= 9: 
     return chr(hexValue + ord('0')) 
    else: # 10 <= hexValue <= 15 
     return chr(hexValue - 10 + ord('A')) 


def main(): 
    # Prompt the user to enter a decimal integer   
    data_file = [] 
    char_file = [] 
    ascii_file = [] 
    hex_key = [] 
    decimal_key = [] 
    nonkey_val = 32 
    data_file.append(' Dec Hex Char ') 
    data_file.append('+---------------+') 
    for i in range(nonkey_val): 
     a_char = chr(i) 
     hex_convert = decimalToHex(i) 
     if i < 10: 
      decimal_key = '0%s' % i 
     else: 
      decimal_key = '%s' % i   
     if i <= 15:    
      hex_key = '0%s' % hex_convert 
     else:    
      hex_key = hex_convert 
     data_file.append('| %s %s %s |' % (decimal_key.strip(), hex_key.strip(), a_char)) 
     # data_file.append('%s' % (a_char)) 

    with open ('sample_file.txt', 'r') as f:      
     data = f.readlines()     
     for character in data:       
      print ('character is %s' % character)    
      decimalValue = charASCII(character[0])    
      hex_convert = decimalToHex(decimalValue)    
      print ('decimalValue is %s' % decimalValue)          
      print ('The hex number for decimal %s is %s' % (decimalValue, hex_convert)             
      data_file.append('| %s %s %s |' % (decimalValue, hex_convert.strip(), character.strip())))            
    data_file.append('+---------------+') 
    print data_file  
    f.close() 

    with open ('output_file.txt', 'w+') as o: 
     for line in data_file: 
      o.write('%s\n'% line) 
    o.close 




main() # Call the main function 
rows = input("Enter the numer of rows: ")  
columns = input("Enter the number of columns: ")  
printRect (rows, columns) 
+1

그리고 어떤 오류가 있습니까? 문제가있는 부분 만 코드를 줄일 수 있습니까? –

+1

미래를위한 팁 : Python 2.x에서 (작동중인) 코드를 3.x로 이식하려면'2to3' 도구가 작업량의 90 %를 처리 할 수 ​​있습니다. 어떤 경우에는 아무것도 편집하지 않아도됩니다. –

답변

0

당신은 추적 코드와 관련 코드 조각을 제공하면이를 분명하게 알 수있는 3 가지 오타가있었습니다. 수동 필요가 없습니다에

with open ('sample_file.txt', 'r') as f:      
    data = f.readlines()     
    for character in data:       
     print ('character is %s' % character)    
     decimalValue = charASCII(character[0])    
     hex_convert = decimalToHex(decimalValue)    
     print ('decimalValue is %s' % decimalValue)          
     print ('The hex number for decimal %s is %s' % (decimalValue, hex_convert))# missing paren here 
     data_file.append('| %s %s %s |' % (decimalValue, hex_convert.strip(), character.strip())) # had extra paren here 
data_file.append('+---------------+') 
print(data_file ) # missing parens 

이 파일을 닫지 만 당신이 있다면 그것은 것 o.close()하지 o.close

나는 것 또한 실제 부울이 아닌 문자열 "참":

 end_row_flag = True  
    if end_row_flag: 
+0

data_file이 강조 표시된 구문 오류입니다. '% s % s % s'% (decimalValue, hex_convert.strip(), character.strip()) print ('십진수 % s의 16 진수는 % s입니다.'% (decimalValue, hex_convert) data_file.append))))))) – Miteux

+0

오류는 나중의 라인에서 나 왔으며 게시 한 코드는 수정 된 버전입니다. –