실수로 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)
그리고 어떤 오류가 있습니까? 문제가있는 부분 만 코드를 줄일 수 있습니까? –
미래를위한 팁 : Python 2.x에서 (작동중인) 코드를 3.x로 이식하려면'2to3' 도구가 작업량의 90 %를 처리 할 수 있습니다. 어떤 경우에는 아무것도 편집하지 않아도됩니다. –