How to create patterns in Python using nested loops? - python

I am trying to create this pattern in Python:
##
# #
# #
# #
# #
# #
I have to use a nested loop, and this is my program so far:
steps=6
for r in range(steps):
for c in range(r):
print(' ', end='')
print('#')
The problem is the first column doesn't show up, so this is what is displayed when I run it:
#
#
#
#
#
#
This is the modified program:
steps=6
for r in range(steps):
print('#')
for c in range(r):
print(' ', end='')
print('#')
but the result is:
#
#
#
#
#
#
#
#
#
#
#
#
How do I get them on the same row?

Replace this...:
steps=6
for r in range(steps):
for c in range(r):
print(' ', end='')
print('#')
With this:
steps=6
for r in range(steps):
print('#', end='')
for c in range(r):
print(' ', end='')
print('#')
Which outputs:
##
# #
# #
# #
# #
# #
It's just a simple mistake in the program logic.
However, it is still better to do this:
steps=6
for r in range(steps):
print('#' + (' ' * r) + '#')
To avoid complications like this happening when using nested for loops, you can just use operators on the strings.

Try this simpler method:
steps=6
for r in range(steps):
print '#' + ' ' * r + '#'

You forgot the second print "#". Put it before the inner loop.

Try something like this:
rows=int(input("Number"))
s=rows//2
for r in range(rows):
print("#",end="")
print()
for r in range(rows):
while s>=0:
print("#"+" "*(s)+"#")
s=s-1
print("#")

Related

Printing a square pattern in Python with one print statement

I want to print a square pattern with "#", the output should look something like this:
# # # # #
# #
# #
# #
# # # # #
The code I was able to write is this:
n=10
for i in range (1,6):
for j in range(1,6):
if i ==1 or 1==n or j==1 or j==n:
print("#", end= ' ')
else:
print(" ", end="")
print()
The output that came is this:
# # # # #
#
#
#
#
I also would like to know if it's possible to only have one print statement instead of many.
Thank you.
This works!
s=5
print(" ".join("#"*s))
for i in range(0,s-2):
print("#"+" "*(s+2)+"#")
print(" ".join("#"*s))
>>>
# # # # #
# #
# #
# #
# # # # #
Single line:
print(" ".join("#"*s)+"\n"+("#"+" "*(s+2)+"#"+"\n")*(s-2)+" ".join("#"*s))
>>>
# # # # #
# #
# #
# #
# # # # #
A simple one-liner for this with a whole square filled in could be
size = 6
print('\n'.join([' '.join('#' * size)] * size))
Or if you want the same pattern in the original
print(' '.join('#' * size))
print('\n'.join('#' * (size - 1)))
I noticed the empty spaces in between the hashtags, use this function it should give you custom dimensions too
def createSquare(xLen,yLen):
for i in range(1,yLen):
if i == 1 or i == yLen-1:
print("# "*xLen)
else:
print("#"+" "*int(xLen*2-3)+"#")
createSquare(10,10)

Python 3 - convert the staircase from right-aligned to LEFT ALIGNED, composed of # symbols and spaces

sc = []
n = 6
for i in range(n):
sc.append("#")
scstr = ''.join(map(str, sc))
print(scstr)
I tried to use the code below to reverse the output by adding padding white spaces but it prints out a distorted staircase.
# print(scstr.rjust(n-i, ' ')) -- trying to print reversed staircase
Please help convert the staircase from right-aligned to LEFT ALIGNED, composed of # symbols and spaces.
Attached is a visual description of expected output
You can use str.rjust()
for i in range(1,n+1):
print( ('#'*i).rjust(n))
I like the new string formatting.
Code:
for i in range(10, -1, -1):
print("{0:#<10}".format(i*" "))
Produces:
#
##
###
####
#####
######
#######
########
#########
##########
You can "right align" a row by padding it with spaces. Here, the Ith rows should have N-I spaces and I hashes:
for i in range(1, n + 1):
print(' ' * (n - i) + '#' * i)
replace n with n+1
n = 6
for i in range(n+1):
print(' '*(n-i) + '#' * i)

How do you make this shape in Python with nested loops?

Write a program using nested loops to draw this pattern. (14) bottom page
for row in range(6): # this outer loop computes each of the 6 lines
line_to_print = '#'
for num_spaces in range(row): # this inner loop adds the desired number or spaces to the line
line_to_print = line_to_print + ' '
line_to_print = line_to_print + '#'
print line_to_print
prints this output:
##
# #
# #
# #
# #
# #

python nested loop print spaces between two characters every repeat

this is the question i am trying to solve
i have tried everything to get the spaces to appear between the hashtags but have failed. i don't know what else to do
this is what i have done so far, i found a few ways to get only 1 space between the hashtags, but to have them repeat every time is what i have not been able to do
star = 6
for r in range(star):
for c in range(r - 5):
print ' ',
print '##',
print
this is the output i get
any help is appreciated.
def hashes(n):
for i in range(n):
print '#' + ' '*i + '#'
Testing
>>> hashes(1)
##
>>> hashes(4)
##
# #
# #
# #
Obviously, there are more succinct ways of doing this, but the original question called for nested loops:
import sys
inner = 1
for x in range(6):
sys.stdout.write('#')
for y in range(inner):
if y == inner - 1:
sys.stdout.write('#')
else:
sys.stdout.write(' ')
sys.stdout.write('\n')
inner += 1
Output:
$ python loops.py
##
# #
# #
# #
# #
# #

Python nested loop triangle

The output I am trying to achieve is :
##
# #
# #
# #
# #
# #
The code I have is :
NUM_STEPS = 6
for r in range(NUM_STEPS):
for c in range(r):
print(' ', end='')
print('#','\t')
print('#')
Its close, but not quite the output I am trying to achieve. Any help or suggestions are most appreciated.
The main thing is you should use '+' (or concat) to build up a string before printing it.
You can eliminate the inner loop by using '*' to make r spaces, which cleans things up a lot.
NUM_STEPS = 6
for r in range(NUM_STEPS):
print("#" + (' ' * r) + "#")
This seemed to work when I tried it:
for r in range(NUM_STEPS):
print("#", end = "")
for c in range(r):
print(" ", end = "")
print("#")
I hope it helps.

Categories

Resources