ck29
ck29

Reputation: 33

Creating pyramid using symbols in Python

This is the assignment:

Write a python function that accepts a character and an integer and then uses that character to create a triangular structure like the example below. Make sure that the number of lines is in the range 1 to 10 and that only the first character in the user entered symbol is used if they enter more than one character.

Symbol? * Lines? 4

    *
   * *
  * * *
 * * * *

I've got all of it except the spacing right... here's what I figured out so far.

def Triangle():
    lines = -1
    while lines not in range(1,11):
        symbol=input("Symbol? ")
        lines=input("Lines? ")
    for i in range(lines + 1):
        spaces = lines - i
        print ((' ' * spaces) + (symbol * i))

This prints out:

     *
    **
   ***
  ****

Can't seem to get this right... thoughts? Also if anyone has ideas on how to ensure only the first character is used as the symbol as noted in the question, that'd be awesome.

Upvotes: 2

Views: 1346

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122012

You need add in spaces after each symbol:

print ((' ' * spaces) + ((symbol + ' ') * i))

Upvotes: 1

Related Questions