Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASCII art in Python

Tags:

python

ascii

I'm pretty new to python, picked it up as an hobby interest, and through some searching found myself a bunch of exercises from "The Practice of computing", one of them asks about writing an ASCII figure, like the one denoted below.

ascii cross

It all seems like an easy enough exercise, but i can't seem wrap my head around the use of a number to draw this, the exercise states that the above drawing was drawn through use of the number "1".

It also states that no number under 0 or above 100 can or should be used to create an ASCII drawing.

Here's another example:

The input here was the number "2".

bigger ascii cross

I've found a way to make the first image appear, but not through any use of the given numbers in any way, just a simple "else" inside a while loop so i could filter out the numbers that are below or equal to 0 and higher or equal to 100.

I've hit a dead stop.

My code as stated above that does not use the variable number to create the first drawing :

while True:
s = input("Give me a number to make a drawing with that is between 0 and 100: ")


if not s.isdigit():
    print ("Error, only numbers will make this program run.")
    continue #Try Again but with a number this time

if int(s) >= 100:
    print ("The number is bigger than or equal to 100 and won't work. \nDo try again.")
    continue #try again

if int(s) <= 0:
    print ("The number is smaller than or equal to 0 and won't work. \nDo try again.")
    continue #try again

else:
    print ("%5s" %("*" *3),"\n"'%5s' %("* *"),"\n" '%7s' %("*** ***"),"\n" '%7s' %("*     *"),"\n" '%7s' %("*** ***"),"\n" '%5s' %("* *"),"\n" '%5s' %("*" *3))


    print ('Want to make another drawing ?')
    continue #make another drawing

The excercise states the following :

An ASCII Figure of the size $n$ is made up of one or several lines. On each line only spaces and the stars (*) are allowed, after each star on a line no spaces are allowed as such you should end with a "\n" or newline. And then followed by the above stated examples.

My new code example which is dependent on the variable input: Also, in this code example it is set to trigger when the input is 1, I'm still having problems with "enlarging" the entire drawing when I increase the input number.

    while True:

 A = input("Give me a number to make a drawing with that is between 0 and 100: ")
 b = "***"
 c = "*"
 d = " "


 if not A.isdigit():
        print ("Error, only numbers will make this program run.")
        continue #Try Again but with a number this time

 if int(A) >= 100:
        print ("The number is bigger than or equal to 100 and won't work. \nDo try again.")
        continue #try again

 if int(A) <= 0:
        print ("The number is smaller than or equal to 0 and won't work. \nDo try again.")
        continue #try again


 else :
  range(1,99)
 if int(A) == (1) :
  print ((d *((int(A))*2)) + b,)
  print ((d *((int(A))*2))+ c + d + c,)
  print ((d *((int(A))*0))+ b + d + b,)
  print ((d *((int(A))*0))+ c + d*5 + c,)
  print ((d *((int(A))*0))+ b + d + b,)
  print ((d *((int(A))*2))+ c + d + c,)
  print ((d *((int(A))*2)) + b,)

  continue #try again

But i've still got a problam with "growing" the number of spaces inside the ASCII figure alongside the increase of 1 to 2.

As i've got a problem with line 3 as well, because it needs to be denoted along the sides of the console, it should have a spacing of 0 from the side, but it must increase to a spacing of 2 with the number 2.

like image 840
Beaver Avatar asked Oct 25 '12 19:10

Beaver


People also ask

What is ASCII in Python with example?

ASCII stands for American Standard Code for Information Interchange. It is a numeric value given to different characters and symbols, for computers to store and manipulate. For example, the ASCII value of the letter 'A' is 65.

Is there ASCII code in Python?

In Python 3, the ord () function works to obtain the ASCII value of a character. The function requires a character to be passed as its parameter. It will return the corresponding ASCII value.


2 Answers

Think about the difference between 1 and 2. Try to draw by hand what 3 and 4 should look like to make the sequence work. Think about it like one of those problems where you are given the start of a sequence and you have to work our the rest.

Like:

0 1 1 2 3 5 8 13

If you don't recognize that right off, it is the Fibonacci sequence. Once you figure out the pattern you can write an arbitrarily long sequence of the values.

And think about this simple ascii sequence:

1)

#

2)

##
#

3)

###
##
#

What does 4) look like?

Or another ascii sequence:

1)

#

2)

 #
# #
 #

3)

  #
 # #
#   #
 # #
  #

What is (4)?

If it still doesn't make sense, try designing a few recursive shapes of your own which are a little similar to the one you are trying to figure out (maybe something along the lines of my second example). Don't worry about how to code it for now, just worry about what the output should be. Then look at the patterns and come out with an algorithm.

like image 60
jmh Avatar answered Sep 24 '22 18:09

jmh


First, analyze the figure line-by-line to identify the different types of patterns.

  • The cap, which appears only on the top and bottom lines. It is any number of spaces, followed by three stars.
  • The wall, which forms vertical sections of the figure. It is any number of spaces, followed by one star, followed by any number of spaces, followed by a star.
  • The floor, which forms horizontal sections of the figure. It is any number of spaces, followed by three stars, followed by any number of spaces, followed by three stars.

We can write a function that prints each of these patterns.

def cap(spacesBefore):
    print " " * spacesBefore + "***"

def wall(spacesBefore, spacesBetween):
    print " " * spacesBefore + "*" + " " * spacesBetween + "*"

def floor(spacesBefore, spacesBetween):
    print " " * spacesBefore + "***" + " " * spacesBetween + "***"

Next, write code that will display a figure of size, 0, 1, and 2. This should give you insight on how to display a figure of any size.

#size 0
cap(0)
wall(0,1)
cap(0)

print "\n"

#size 1
cap(2)
wall(2, 1)
floor(0, 1)
wall(0, 5)
floor(0, 1)
wall(2, 1)
cap(2)

print "\n"

#size 2
cap(4)
wall(4, 1)
floor(2, 1)
wall(2, 5)
floor(0, 5)
wall(0, 9)
floor(0, 5)
wall(2, 5)
floor(2, 1)
wall(4, 1)
cap(4)

Output:

***
* *
***


  ***
  * *
*** ***
*     *
*** ***
  * *
  ***


    ***
    * *
  *** ***
  *     *
***     ***
*         *
***     ***
  *     *
  *** ***
    * *
    ***

Analyzing the code used to make these figures, some patterns become apparent. For a figure of size N:

  • Both end caps have N*2 preceding spaces.
  • There are 2*N+1 wall lines.
  • There are 2*N floor lines.
  • The first and second halves of the figure are mirror images.
  • The number of preceding spaces for each wall line begins at N*2, then shrinks by two until it reaches zero; then it grows by two again until it reaches N*2 once more.
  • The number of spaces between walls begins at 1, and increases by 4 until it reaches 4*N+1; then it shrinks by four again until it reaches 1 once more.
  • The number of preceding spaces for each floor begins at 2N-2, then shrinks by two until it reaches zero; then it grows by two again until it reaches 2N-2 once more.
  • The number of spaces between floors begins at 1, and increases by 4 until it reaches 4*N-3; then it shrinks by four again until it reaches 1 once more.

The patterns all grow and shrink at a linear rate, and then shrink and grow at a linear rate. This implies that we should use two for loops with opposite conditions, with a little extra code for the caps and central wall.

def draw(N):
    cap(2*N)
    for i in range(N):              #loop from 0 to N-1
        wall(2*(N-i), 1+(4*i))
        floor(2*(N-i-1), 1+(4*i))
    wall(0, 4*N+1)
    for i in range(N-1, -1, -1):    #loop from N-1 to 0
        floor(2*(N-i-1), 1+(4*i))
        wall(2*(N-i), 1+(4*i))
    cap(2*N)

Now test the code.

for i in range(7,10):
    draw(i)
    print "\n"

Output:

              ***
              * *
            *** ***
            *     *
          ***     ***
          *         *
        ***         ***
        *             *
      ***             ***
      *                 *
    ***                 ***
    *                     *
  ***                     ***
  *                         *
***                         ***
*                             *
***                         ***
  *                         *
  ***                     ***
    *                     *
    ***                 ***
      *                 *
      ***             ***
        *             *
        ***         ***
          *         *
          ***     ***
            *     *
            *** ***
              * *
              ***


                ***
                * *
              *** ***
              *     *
            ***     ***
            *         *
          ***         ***
          *             *
        ***             ***
        *                 *
      ***                 ***
      *                     *
    ***                     ***
    *                         *
  ***                         ***
  *                             *
***                             ***
*                                 *
***                             ***
  *                             *
  ***                         ***
    *                         *
    ***                     ***
      *                     *
      ***                 ***
        *                 *
        ***             ***
          *             *
          ***         ***
            *         *
            ***     ***
              *     *
              *** ***
                * *
                ***


                  ***
                  * *
                *** ***
                *     *
              ***     ***
              *         *
            ***         ***
            *             *
          ***             ***
          *                 *
        ***                 ***
        *                     *
      ***                     ***
      *                         *
    ***                         ***
    *                             *
  ***                             ***
  *                                 *
***                                 ***
*                                     *
***                                 ***
  *                                 *
  ***                             ***
    *                             *
    ***                         ***
      *                         *
      ***                     ***
        *                     *
        ***                 ***
          *                 *
          ***             ***
            *             *
            ***         ***
              *         *
              ***     ***
                *     *
                *** ***
                  * *
                  ***
like image 31
Kevin Avatar answered Sep 24 '22 18:09

Kevin