简体   繁体   中英

Creating an upside down asterisk triangle in Python using for loop

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)

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

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

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.

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:
 *************
  ***********
   *********
    *******
     *****
      ***
       *

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:

****
 ***
 **
  *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM