0
votes

I am making a program which takes a file with A-Z written in ASCII art. The program takes 3 lines of input: Hight, Width and the word that is to be written (All caps and spaces). I have managed to get the program to print the first letter that is iterated but, for some reason, it seems the for loop I set up just stops.

temp = []
hi = input('Height: ')
wi = input('Width: ')
tx = input('Text: ')
fi = open("font.txt")
for n in range(len(tx)):
    for i in tx:
        temp = cd[i]
        var1 = int(temp[0])
        ra1 = (var1 * int(hi))
        ra1n = (ra1 + int(hi))
        temp = []
        lines = fi.readlines()
        print(''.join(lines[ra1:ra1n]), end='')

This is what it outputs

Height: 8
Width: 9
Text: WOW

|\     /|
| )   ( |
| | _ | |
| |( )| |
| || || |
| () () |
(_______)
2
cd is a dictionary. The keys are every upper-case letter in the alphabet. the values are the letters would be positions in the file if hi(height) and wi(width) were equal to 1.user5303899

2 Answers

1
votes

You are trying to reread font.txt without rewinding the file position.

Either add a file.seek(0) call, or read the file once outside of the loop; using lines = fi.readlines() outside of the loop is going to be more efficient than using fi.seek(0) every time.

You also have a loop too many; you don't need to loop over range(len(tx)) here, because you already loop over tx itself.

with open("font.txt") as fi:
    lines = fi.readlines()

for character in tx:
    var1 = int(cd[character])
    ra1 = (var1 * int(hi))
    ra1n = (ra1 + int(hi))
    print(''.join(lines[ra1:ra1n]), end='')
0
votes

Try file.seek(0) in loop before lines = fi.readlines().

    temp = []
    hi = input('Height: ')
    wi = input('Width: ')
    tx = input('Text: ')
    fi = open("font.txt")
    for n in range(len(tx)):
        for i in tx:
            temp = cd[i]
            var1 = int(temp[0])
            ra1 = (var1 * int(hi))
            ra1n = (ra1 + int(hi))
            temp = []
            fi.seek(0) # Here
            lines = fi.readlines()
            print(''.join(lines[ra1:ra1n]), end='')

or open the file in loop.

temp = []
hi = input('Height: ')
wi = input('Width: ')
tx = input('Text: ')
for n in range(len(tx)):
    for i in tx:
        temp = cd[i]
        var1 = int(temp[0])
        ra1 = (var1 * int(hi))
        ra1n = (ra1 + int(hi))
        temp = []
        fi = open("font.txt") # Here
        lines = fi.readlines()
        print(''.join(lines[ra1:ra1n]), end='')