ASCII art in Python

ascii, python

Solution

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.

Problem

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. 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". 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.

Original source

Related problems