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.
Related
I want to print an empty pyramid in python 3 and my teacher suggested me this code but i am confused about the list(array) and i want an alternative of this code. Is there any alternative method to print an empty pyramid. This code is also available on available on stackoverflow but i want to solve it by using simple if else.
#Function Definition
def Empty_triangle(n): # Here value of n is 5
for i in list(range(n-1))+[0]:
line = ""
leadingSpaces = n-2-i
line += " "*leadingSpaces
line += "*"
if i != 0:
middleSpaces = 2*i-1
line += " "*middleSpaces
line += "*"
print(line)
# Function Call
n = 5
Empty_triangle(n)
Example of code which i want
if (row==0 and row==5 and col!=0):
output should like this using ifelse
Can it be done with simple if else
In the code your teacher suggested, the "list" seems redundant. When n=5, the "range(n-1)" command already gives you the numeric list [0, 1, 2, 3, 4]. Since it's already a list, you don't need to put "list()" around it. Then the "+[0]" just adds 0 to the end to give you [0, 1, 2, 3, 4, 0]. Also, I'm not sure if you want that extra star at the bottom center.
There are lots of alternative ways to do it, but here's an alternative version I made:
def triangle2(n):
for row in range(n):
line = [ ' ' for i in range(n+row+1)]
line[n-row] = '*'
line[n+row] = '*'
print ''.join(line)
triangle2(5)
For each row, it creates a list of spaces that's just big enough for that row, then it replaces the spaces with the locations of where the stars should be. Finally it joins all of the spaces and stars into a string, and prints it.
An even shorter way is to essentially take half of each row, then mirror it to make the second half:
def triangle3(n):
for row in range(n):
line = [ ' ' for i in range(n-row) ] + ['*'] + [ ' ' for i in range(row)]
print(''.join(line[:-1] + line[::-1]))
Or skipping lists and joins and just using concatenated strings:
def triangle4(n):
for row in range(n):
first_half = ' '*(n-row) + '*' + ' '*row
second_half = first_half[::-1][1:]
print(first_half + second_half)
The "[::-1]" part is a little trick to reverse a string or list.
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern triangle
def triangle(n):
# number of spaces
k = 2*n - 2
# outer loop to handle number of rows
for i in range(0, n):
# inner loop to handle number spaces
# values changing acc. to requirement
for j in range(0, k):
print(end=" ")
# decrementing k after each loop
k = k - 1
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range(0, i+1):
# printing stars
print("* ", end="")
# ending line after each row
print("\r")
# Driver Code
n = 5
triangle(n)
careful with indent.
previously whole code wasn't displayed.
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)
I have a python program which does a SOAP request to a server, and it works fine:
I get the answer from the server, parse it, clean it, and when I am done, I end up with a string like that:
name|value|value_name|default|seq|last_modify|record_type|1|Detail|0|0|20150807115904|zero_out|0|No|0|0|20150807115911|out_ind|1|Partially ZeroOut|0|0|20150807115911|...
Basically, it is a string with values delimited by "|". I also know the structure of the database I am requesting, so I know that it has 6 columns and various rows. I basically need to split the string after every 6th "|" character, to obtain something like:
name|value|value_name|default|seq|last_modify|
record_type|1|Detail|0|0|20150807115904|
zero_out|0|No|0|0|20150807115911|
out_ind|1|Partially ZeroOut|0|0|20150807115911|...
Can you tell me how to do that in Python? Thank you!
Here's a functional-style solution.
s = 'name|value|value_name|default|seq|last_modify|record_type|1|Detail|0|0|20150807115904|zero_out|0|No|0|0|20150807115911|out_ind|1|Partially ZeroOut|0|0|20150807115911|'
for row in map('|'.join, zip(*[iter(s.split('|'))] * 6)):
print(row + '|')
output
name|value|value_name|default|seq|last_modify|
record_type|1|Detail|0|0|20150807115904|
zero_out|0|No|0|0|20150807115911|
out_ind|1|Partially ZeroOut|0|0|20150807115911|
For info on how zip(*[iter(seq)] * rowsize) works, please see the links at Splitting a list into even chunks.
data = "name|value|value_name|default|seq|last_modify|record_type|1|Detail|0|0|20150807115904|zero_out|0|No|0|0|20150807115911|out_ind|1|Partially ZeroOut|0|0|20150807115911|"
splits = data.split('|')
splits = list(filter(None, splits)) # Filter empty strings
row_len = 6
rows = ['|'.join(splits[i:i + row_len]) + '|' for i in range(0, len(splits), row_len)]
print(rows)
>>> ['name|value|value_name|default|seq|last_modify|', 'record_type|1|Detail|0|0|20150807115904|', 'zero_out|0|No|0|0|20150807115911|', 'out_ind|1|Partially ZeroOut|0|0|20150807115911|']
How about this:
a = 'name|value|value_name|default|seq|last_modify|record_type|1|Detail|0|0|20150807115904|zero_out|0|No|0|0|20150807115911|out_ind|1|Partially ZeroOut|0|0|20150807115911|'
b = a.split('|')
c = [b[6*i:6*(i+1)] for i in range(len(b)//6)] # this is a very workable form of data storage
print('\n'.join('|'.join(i) for i in c)) # produces your desired output
# prints:
# name|value|value_name|default|seq|last_modify
# record_type|1|Detail|0|0|20150807115904
# zero_out|0|No|0|0|20150807115911
# out_ind|1|Partially ZeroOut|0|0|20150807115911
Here is a flexible generator approach:
def splitOnNth(s,d,n, keep = False):
i = s.find(d)
j = 1
while True:
while i > 0 and j%n != 0:
i = s.find(d,i+1)
j += 1
if i < 0:
yield s
return #end generator
else:
yield s[:i+1] if keep else s[:i]
s = s[i+1:]
i = s.find(d)
j = 1
#test runs, showing `keep` in action:
test = 'name|value|value_name|default|seq|last_modify|record_type|1|Detail|0|0|20150807115904|zero_out|0|No|0|0|20150807115911|out_ind|1|Partially ZeroOut|0|0|20150807115911|'
for s in splitOnNth(test,'|',6,True): print(s)
print('')
for s in splitOnNth(test,'|',6): print(s)
Output:
name|value|value_name|default|seq|last_modify|
record_type|1|Detail|0|0|20150807115904|
zero_out|0|No|0|0|20150807115911|
out_ind|1|Partially ZeroOut|0|0|20150807115911|
name|value|value_name|default|seq|last_modify
record_type|1|Detail|0|0|20150807115904
zero_out|0|No|0|0|20150807115911
out_ind|1|Partially ZeroOut|0|0|20150807115911
There are really many ways to do it. Even with a loop:
a = 'name|value|value_name|default|seq|last_modify|record_type|1|Detail|0|0|20150807115904' \
'|zero_out|0|No|0|0|20150807115911|out_ind|1|Partially ZeroOut|0|0|20150807115911|'
new_a = []
ind_start, ind_end = 0, 0
for i in range(a.count('|')// 6):
for i in range(6):
ind_end = a.index('|', ind_end+1)
print(a[ind_start:ind_end + 1])
new_a.append(a[ind_start:ind_end+1])
ind_start = ind_end+1
The print is just to saw the results, you remove it:
name|value|value_name|default|seq|last_modify|
record_type|1|Detail|0|0|20150807115904|
zero_out|0|No|0|0|20150807115911|
out_ind|1|Partially ZeroOut|0|0|20150807115911|
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("#")