2

I hate that I have to ask this, but I can't for the life of me figure out how to make this work. The program is supposed to ask for an input of an odd integer and then create an upside down pyramid with the first row containing the amount of asterisks as the number, and the last row having only one, centered asterisk. I've managed to figure out most of it, but my asterisks refuse to line up centered no matter what I try. I've looked at the other topics similar here, and tried to use them but still cannot figure it out. I'm not sure why 'i' is being used, but saw it on another post and it looked marginally better than what I had before.

Here is my code, I have tinkered with it quite a bit to no avail.

x=input('Enter an odd number width: ')
x_int = int(x)
print('Triangle:')
for i in range(x_int+1, 0, -1) :
    numwhite = (x_int - i)/2
    white_int= int(numwhite)
    print(' '* white_int + '*'*i)

Which outputs (input 13):

Triangle:
**************
*************
************
 ***********
 **********
  *********
  ********
   *******
   ******
    *****
    ****
     ***
     **
      *

I want it to look something like (input 7)

*******
 *****
  ***
   *
2
  • I don't what your constraints are. However, here are two hints: (a) Use a monospace font (so that each character occupies the same amount of horizontal space). (b) Use two character positions for each asterisk, one blank and one the asterisk itself. Commented Oct 5, 2016 at 20:55
  • You have to remove two stars per line - that's range(x_int+1, 0, -2). Commented Oct 5, 2016 at 21:10

4 Answers 4

2

Try something like:

x = int(input('Enter an odd number width: '))
print('Triangle:')
for i in range(x_int, 0, -1):
    print('{:^{str_len}}'.format('* ' * i, str_len= x_int * 2))

That should work not only for odd numbers.

Sign up to request clarification or add additional context in comments.

Comments

2

Apart from setting your step value to -2, you'll have a more readable code if you use Python's string format method. You can easily center your asterisks by using center alignment viz. ^:

x = input('Enter an odd number width: ')
x_int = int(x)
print('Triangle:')

fmt = '{' + ':^{}'.format(x_int) + '}' # Notice the caret ^ and how the width is set
for i in range(x_int, 0, -2):
    stars = '*'*i
    print(fmt.format(stars))

Trial:

Enter an odd number width: 15
Triangle:
***************
 ************* 
  ***********  
   *********   
    *******    
     *****     
      ***      
       *    

You can do more with string formatting. Have a look at the reference: https://pyformat.info

1 Comment

Thanks, I'll look into that. Just beginning coding.
1

You need to set the 'step' of the range to -2, so that it only grabs odds ints in reverse. Then the number of white spaces needed to have a + 1 to work properly.

x=input('Enter an odd number width: ')
x_int = int(x)
print('Triangle:')
for i in range(x_int, 0, -2):
    numwhite = ((x_int - i)/2) + 1
    white_int= int(numwhite)
    print(' ' * white_int + '*'*i)

Ouput:

Enter an odd number width: 7
Triangle:
*******
 *****
  ***
   *

More readable code with explanations

x_int = int(input('Enter an odd number width: '))  # reducing

print('Triangle:')

for i in range(x_int, 0, -2):  # -2 means only do every 2nd number in reverse
    num_white = int(((x_int - i)/2) + 1)  # Checking the math here, needed a +1
    print(' ' * num_white + '*' * i)  # Could be changed to .format but works

Output

Enter an odd number width: 13
Triangle:
 *************
  ***********
   *********
    *******
     *****
      ***
       *

1 Comment

Thank you! Feel dumb that it was such a simple fix, but much appreciated.
0

I don't think you can center them due to not being able to place characters between half-spaces. Maybe try inserting a space between all stars so you can center them better and produce something like this.

* * * *
 * * *
  * * 
   *

Instead of:

****
 ***
 **
  *

Comments

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.