I am trying to write a program that looks something like this if, say, the input number was 6, or something like that the output should look like this:
*
**
***
****
*****
******
*****
****
***
**
*
but when I do it like I was told, this way specifically because this is what a classmate told me to do. :
n = int(input("Enter a value for n: "))
for i in range(1, n + 1):
for j in range(n):
if n - j > i:
print(" ", end = " ")
else:
print("*", end = " ")
print("")
for i in range(1, n):
for j in range(n):
if n - j < i:
print(" ", end = " ")
else:
print("*", end = " ")
print("")
I get:
*
**
***
****
*****
******
*****
****
***
**
*
What am I doing wrong? Please tell me how to get it to correctly line up, I'd really appreciate it if someone could help me with this so I can learn to do this on my own, please assist me...
If your assignment requires you to write the code exactly as you posted, Austin Kootz answer is the way to go.
However, a more simplifed way of doing this is using ljust
n = 6
for x in range(n - 1, 0, -1):
print ''.ljust(x, ' ') + '*'.ljust(n - x, '*')
for x in range(n):
print ''.ljust(x, ' ') + '*'.ljust(n - x, '*')
Your loops are a bit overcomplicated, so I've simplified somewhat:
n = int(input("Enter a value for n: "))
for x in range(n):
out = ''
for y in range(n-x):
out = out +' '
for y in range(x):
out = out +'*'
print(out)
for x in range(n):
out = ''
for y in range(x):
out = out +' '
for y in range(n-x):
out = out +'*'
print(out)
Enjoy!
What you want in the second set of loops is to take the row number (counting from 1) and print that many spaces (" "), and then print asterisks ("*") for the rest of the row. So if i is the row number and j the column number (and indexing starts from 0), you should print " " while j < i + 1 and "*" otherwise. This gives:
# The top part of the pyramid
for i in range(1, n + 1):
for j in range(n):
if n - j > i:
print(" ", end = " ")
else:
print("*", end = " ")
print("")
# The bottom half of the pyramid
for i in range(n):
for j in range(n):
# Print spaces in the beginning of the row
# (while the column number is less than the row number)
if j < i + 1:
print(" ", end = " ")
# Print asterisks for the rest of the row
else:
print("*", end = " ")
print("")
Related
These are what I'm currently working with:
row = int(input('Enter number of rows required: '))
for i in range(row,0,-1):
for j in range(row-i):
print('*', end='')
for j in range(2*i-1):
print('0',end='')
print()
And this is the current output:
0000000
*00000
**000
***0
I can't figure out what to change to have the correct output
Add another asterisk-printing loop before printing a newline.
row = 4
for i in range(row, 0, -1):
for j in range(row - i):
print('*', end='')
for j in range(2 * i - 1):
print('0', end='')
for j in range(row - i):
print('*', end='')
print()
prints out
0000000
*00000*
**000**
***0***
(For a more Pythonic solution, you could just
row = 4
full_width = 2 * row - 1
for i in range(row, 0, -1):
row_zeroes = (2 * i - 1)
print(("0" * row_zeroes).center(full_width, '*'))
but I suspect that's not the point of this exercise.)
you can use f-string and siple nth number from the formula a + (n-1)*d
row = int(input('Enter number of rows required: '))
for i in range(row):
print(f'{"*"*(i+1)}{"0"*(2*(row-i-1) - 1)}{"*"*(i+1)}')
By Using Loop, Print
*
*
*
*
Print hollow square by using loops in python.
You may use below function to generate hollow square of side length "side"
def hollow_square(side):
for i in range(side):
for j in range(side):
if i == 0 or i == side-1 or j == 0 or j == side-1:
print("*", end="")
else:
print(" ", end="")
print()
hollow_square(5)
you could do it in this way
n = int(input("enter the number:"))
def spaces(n): # creating spaces
sp = ""
for i in range(n-2):
sp += " "
return sp
for i in range(n):
if(i == 0) : # for top row
print('*'*n)
elif (i==n-1) : #for bottom row
print('*'*n)
else : # for the middle section
print(f"*{spaces(n)}*")
I have to print a hollow inverted pyramid:
******
* *
* *
* *
**
*
Following is my code:
n = int(input())
for i in range(n,0,-1):
if i == n:
print(n*'*', end = '')
if i > 1 and i <n:
print('*'+(i-2)*' '+'*')
else:
print('*')
print()
For input as 6 I am not sure why my code is printing 7 stars.
If anyone could help explain what I am doing wrong or missing would be really great!
There are two problems here:
There is no need for the , end=''. You still want a newline to print after this line of stars.
You used if not elif for the second condition, so the third block of code in the else will still run even if the first condition is true.
Here is the corrected code:
n = int(input())
for i in range(n, 0, -1):
if i == n:
print(n * "*")
elif 1 < i < n:
print("*" + (i - 2) * ' ' + '*')
else:
print('*')
If the first iteration of the loop, there are two print calls executing: the first one and the last, so that makes a total of 7 stars in that first line of output.
As the first and last case are different from the other cases, it is easier to just deal with those outside of the loop:
n = int(input())
print(n*'*')
for i in range(n - 3, -1, -1):
print('*' + i*' ' + '*')
if n > 1:
print('*')
There is still one if here to ensure it still works correctly for n equal to 1 or even 0.
To do it without that if, you could do this:
n = int(input())
print(n*'*')
s = '*' + n*' '
for i in range(n - 2, -1, -1):
print(s[:i] + '*')
another type of inverted hollow matrix:
54321
4 1
3 1
2 1
1
Answers:
Hollow Right-angled triangle!!!
n=int(input())
for i in range(n,0,-1):
for k in range(n,0,-1):
if i==n or i==k or k==n-(n-1):
print(k,end=" ")
else:
print(end=" ")
print()
I am new on Python and I am following a book that purposes the following excercise:
Write a program to generate the following pattern in Python:
*
**
***
****
The suggested code is:
n = input('Enter the number of rows: ')
m = int(n)
*k = 1
for i in range(m):
for j in range(i, i + 2):
print('*', end = " ")
print()
and enter n=5.This lead me to ask to questions. The first one is the *k=1, I am asumming the '' is a typo on the book since my program does not run with it. However i am not seeing that k is being used within the loop. My second question is that I do not understand why my outcome is different than the one I get (once removed the ''). This is what I get when n=5:
**
**
**
**
**
This works for your problem. In python, you can multiply strings. It will be useful for the problem. *k is a typo.
n = input(' num rows: ')
n = int(n)
for i in range(1, n + 1):
print ('*' * i)
You can try this solution. I am pretty sure the *k = 1 is a typo.
n = int(input('Enter the number of rows: '))
k = 1
for i in range(n):
print(k * '* ', end = "\n")
k += 1
Another approach if you don't want to use multiplication approach:
n = input('Enter the number of rows: ')
m = int(n)
for i in range(m):
for j in range(1, i + 2):
print('*', end = " ")
print()
def printStarsInTriangeForm(count):
for i in (range(1, count + 1)):
print("*" * i)
This is one way. :)
yea *k = 1 cant be right, you can delete it.
Your mistake or the mistake in the book is the line:
for j in range(i, i + 2):
if you type i * 2 it works:
for j in range(i, i * 2):
and if you want no spaces between the starts you need to change the print in the loop to:
print("*",end="")
i just removed the space in the "end".
and a better way you can do this is
m = int(input('Enter the number of rows: '))
for i in range(m + 1):
print(i * "*")
I am trying to create an arrow out of asterisk's, where the amount of columns is entered by the user. Yes, I do know how to use for loops to accomplish this:
columns = int(input("How many columns? "))
while columns <= 0:
print ("Invalid entry, try again!")
columns = int(input("How many columns? "))
x = 1
for x in range(1, columns):
for x in range(x):
print(" ", end="")
print("*")
for x in range(columns,0,-1):
for x in range(x):
print(" ", end="")
print("*")
#output looks like
"""
How many columns? 3
*
*
*
*
*
"""
However my question is, how would I accomplish the same outcome using only while loops?
Thanks
Edit: I was going to post what I had thus far in trying to work it out myself, but it is now of no use!
Thank you all for your efficient varying answers! Much appreciated!
Just for fun, here's a version that doesn't loop using indexing.
def print_arrow(n):
a = '*'.ljust(n + 1)
while a[-1] != '*':
print(a)
a = a[-1] + a[:-1]
a = a[1:]
while a[0] != '*':
a = a[1:] + a[0]
print(a)
# Test
print_arrow(4)
output
*
*
*
*
*
*
*
This should do:
columns = int(input("How many columns? "))
while columns <= 0:
print ("Invalid entry, try again!")
columns = int(input("How many columns? "))
x = 1
while x < columns:
y = 0
while y < x:
print(" ", end="")
y += 1
print("*")
x += 1
x = columns
while x > 0:
y = 0
while y < x:
print(" ", end="")
y += 1
print("*")
x -= 1
First, it's better to use functions. And easier if you know that character*number returns that character concatenated number times.
Example:
'*'*10
returns
'**********'
So your program using whiles would follow the same logic.
def print_arrow(k):
i = 0
while(i < k-1):
print(i*' ' + '*')
i +=1
while(i >= 0):
print(i*' ' + '*')
i -= 1
The first while prints the upper part, the last one uses the fact that i = k-1, so just do same in the reversed order.
Example:
print_arrow(3)
returns
*
*
*
*
*
n = int(input( ))
n1 = n//2 + 1
i = 1
while i <= n1:
space = 1
while space <= i - 1:
print(" ",end="")
space += 1
j = 1
p = "*"
while j <= i:
if j == i:
print(p,end="")
else:
print("* ",end="")
j += 1
print()
i += 1
i = n - n1
while i >= 1:
space = 1
while space <= i - 1:
print(" ",end="")
space += 1
j = 1
p = "*"
while j <= i:
if j == i:
print(p,end="")
else:
print("* ",end="")
j += 1
print()
i -= 1
Arrow pattern of asterisk
-Remember the n value here is always odd
for n = 5
output will be