Reputation: 3187
What I mean, I want to make a program that prints stars according to the user input and I did it:
for i in range(num):
print((i*-1)*' ' + (i+1)*'*')
for o in range(num-1):
print((num-o-1)*'*')
except instead of this:
*
* *
*
I get this:
*
**
*
How do I implement the spaces? Thank in advance!
Upvotes: 0
Views: 59
Reputation: 39451
If you multiply a string by a negative number, you just get the empty string. And your formulas aren't correct to begin with. You need to pad the front with enough spaces to make it line up (in a monospaced terminal).
Here's one way to do it correctly
>>> def diamond(n):
... print(' '*n + '*')
... for i in list(range(1,n)) + list(range(n,0,-1)):
... print('{}*{}*'.format(' '*(n-i), ' '*(2*i-1)))
... print(' '*n + '*')
Converting the ranges to lists isn't efficient for large n, but it's not a bottleneck and the terminal only practically supports widths up to 80 characters anyway.
Upvotes: 2