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:
##
# #
# #
# #
# #
# #
Related
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)
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)
So I am trying to create a grid that can have it's individual "grid squares" replaced by any given symbol. The grid works fine but it is made of lists within lists.
Here's the code
size = 49
feild = []
for i in range(size):
feild.append([])
for i in range(size):
feild[i].append("#")
feild[4][4] = "#" #This is one of the methods of replacing that I have tried
for i in range(size):
p_feild = str(feild)
p_feild2 = p_feild.replace("[", "")
p_feild3 = p_feild2.replace("]", "")
p_feild4 = p_feild3.replace(",", "")
p_feild5 = p_feild4.replace("'", "")
print(p_feild5)
As you can see that is one way that I have tried to replace the elements, I have also tried:
feild[4[4]] = "#"
and
feild[4] = "#"
The first one replaces all "#" 4 elements in from the left with "#"
The second one gives the following error
TypeError: 'int' object is not subscriptable
The make a grid of # with row 3, column 3 replaced with #:
>>> size = 5
>>> c = '#'
>>> g = [size*[c] for i in range(size)]
>>> g[3][3] = '#'
>>> print('\n'.join(' '.join(row) for row in g))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
May be you're looking for this :-
size = 49
feild = []
for i in range(size):
feild.append([])
for i in range(size):
map(feild[i].append, ["#" for _ in xrange(size)])
i = 4
feild[i][0] = "#"
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
##
# #
# #
# #
# #
# #
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("#")