目标是将存储文本文件的RLE转换为在控制台中打印的ASCII Art。用户必须输入文本文件的名称。每次尝试更改或改进代码时,都会出现错误。
代码:
def convtoASCII():
print("\nYou have chosen Convert to ASCII\n")
name = input("\nWhat is the name of your RLE file?")
try:
lines = len(open(name).readlines( ))
for _ in range(lines):
line = lines
piece = [line[i:i+3] for i in range(0, len(line), 3)]
for part in piece:
length = int(part[0:2])
character = part[2]
print(length * character, end='')
except:
print("\nInvalid file name. Please try again\n")
convtoASCII()
这是我收到的错误消息:
Traceback (most recent call last):
File "C:\Users\ayaan\Documents\RLE to ASCII\Raw Code.py", line 100, in <module>
welcome()
File "C:\Users\ayaan\Documents\RLE to ASCII\Raw Code.py", line 8, in welcome
menu()
File "C:\Users\ayaan\Documents\RLE to ASCII\Raw Code.py", line 19, in menu
convtoASCII()
File "C:\Users\ayaan\Documents\RLE to ASCII\Raw Code.py", line 83, in convtoASCII
piece = [line[i:i+3] for i in range(0, len(line), 3)]
TypeError: object of type 'int' has no len()
任何帮助将不胜感激
谢谢