Solutions

Exercise: Create a flow chart for a simple calculator

Arrow Challenge

Write a function draw_arrow(n) that takes an integer n and prints the following pattern:

n = 3

  *
 ***
*****
  *
  *

n = 5

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

Solution:

def draw_arrow(n):
    for i in range(n):
        #number of stars:
        stars = (i*2 +1) * "*"  # 1, 3, 5, 7, ...
        #number of spaces
        spaces = ((n-i) - 1) * " " # n-1, n-2, n-3, ...
        #print row
        print(spaces+stars)
    
    #print shaft
    shaft_spaces = (n-1) * " "
    print(shaft_spaces+"*")
    print(shaft_spaces+"*")

Try it out here

Last updated

Was this helpful?