1

I'm doing an ascii problem in Python. The goal is to create an image using certain values to create certain images:

The value 0 creates a "-" and moves the cursor to the right for the next variable. 1 creates a "/" and moves the cursor up one row and to the right. 2 creates a "|" and moves the cursor up one row in the same position. 3 creates a "\" and moves the cursor up one row and over to the left one space. 4 creates a "-" and moves the cursor left. 5 creates a "/" and moves the cursor down one row and to the left. 6 creates a "|" and moves the cursor down one row. 7 creates a "\" and moves the cursor down one row and to the right.

However I'm not sure how to print this to make it happen. My code:

file_object = open("input.txt","w")

file_object.write("770000334444")
file_object.write("2255500004666")
file_object.write("002444")

file_object.close()

file_object = open("input.txt","r")


while True:
    c = file_object.read(1)
    if c == "0":
        print("-")
    if c == "1":
        print("/")
    if c == "2":
        print("|")
    if c == "3":
        print("\")
    if c == "4":
        print("-")
    if c == "5":
        print("/")
    if c == "6":
        print("|")
    if c == "7":
        print("\")
cdlane
  • 40,441
  • 5
  • 32
  • 81

3 Answers3

0

Congratulations on tackling a fun problem. Because you have an open ended question, I am providing some general information in the hopes it might help you.

The print function can print without always going to the next line. For example, this will print "Hello".

print("Hel", end="")
print("lo")

You can use the turtle module for graphical programs kind of like you want. You might want to look at the module documentation

You can use the curses module to draw characters wherever you want. It is a bit harder to use than turtle but does draw characters. You probably want to start with the How-To guide.

You can create your own list of lines of characters, fill them in, and print them out. This is a much harder approach and it doesn't use any libraries.

Have fun! Keep coding! Keep notes.

Charles Merriam
  • 19,908
  • 6
  • 73
  • 83
0

if you are running it in linux, than most terminals understand ANSI escape codes. The relevant codes for this use case:

  • "\033[F" – move cursor to the beginning of the previous line
  • "\033[A" – move cursor up one line

Example (Python):

print("\033[FMy text overwriting the previous line.")

But if your'e using windows you will need to use a module that enables ansi escape codes.

You could check Python module to enable ANSI colors for stdout on Windows? to see if it's useful.

The colorama module seems to be cross-platform.

You install colorama:

pip install colorama

and than try using the ANSI escape codes in your code. :)

avrum
  • 135
  • 1
  • 10
0

In your code, the print("\") won't work as '\' is the escape character so you need to do print("\\") instead.

I think it's a mistake to jump into writing code -- to me this is a data structure problem. And the better the data structure, the less code you need to write. Here's how I might solve this problem using Python turtle:

from turtle import Screen, Turtle

FONT_SIZE = 18
FONT = ('Arial', FONT_SIZE, 'normal')

OPERATORS = [
    ["-",  ( 1,  0)],  # 0 draws "-" and moves cursor to the right
    ["/",  ( 1,  1)],  # 1 draws "/" and moves cursor up one row and to the right
    ["|",  ( 0,  1)],  # 2 draws "|" and moves cursor up one row in the same position
    ["\\", (-1,  1)],  # 3 draws "\" and moves cursor up one row and over to the left
    ["-",  (-1,  0)],  # 4 draws "-" and moves cursor left
    ["/",  (-1, -1)],  # 5 draws "/" and moves cursor down one row and to the left
    ["|",  ( 0, -1)],  # 6 draws "|" and moves cursor down one row
    ["\\", ( 1, -1)],  # 7 draws "\" and moves cursor down one row and to the right
]

def display(turtle, string):
    for digit in string:
        character, (dx, dy) = OPERATORS[int(digit)]
        x, y = turtle.position()
        turtle.write(character, align='center', font=FONT)
        turtle.goto(x + dx * FONT_SIZE, y + dy * FONT_SIZE)

if __name__ == "__main__":
    turtle = Turtle()
    turtle.hideturtle()
    turtle.penup()

    display(turtle, "2255500004666")

    screen = Screen()
    screen.exitonclick()

OUTPUT

enter image description here

cdlane
  • 40,441
  • 5
  • 32
  • 81