2

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.

2
  • +1 I've seen this assignment posted dozens of times here, it's the first time OP really tried something :) Commented Jan 14, 2014 at 19:18
  • Are you using Python 2 or 3? If 3, your input() call will return a string and your while loop will continue endlessly. If Python 2, you need to enter '*' for the symbol, so you may want to use raw_input() there instead. Commented Jan 14, 2014 at 19:21

1 Answer 1

1

You need add in spaces after each symbol:

print ((' ' * spaces) + ((symbol + ' ') * i))
Sign up to request clarification or add additional context in comments.

4 Comments

This seems very wrong? His initial spacing is correct and the *i binds with ' ' at the moment.
I was using Python 3, and thank you so much. I've been staring at this computer for 30 minutes. Huge help!
@k29: Which means that you need to use lines = int(input("Lines? ")) for your code to work.
yeah I already added that in based on your previous comment. Thanks! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.