How to print the pattern like this:
(need to print n-1 lines)
input=3
----#
--#-#-#
input=6
----------#
--------#-#-#
------#---#---#
----#-----#-----#
--#-------#-------#
My code:
row = int(input())
for i in range(1, row):
for j in range(1,row-i+1):
print("-", end="")
for j in range(1, 2*i):
if j==1 or j==2*i-1:
print("#", end="")
else:
print("-", end="")
print()
MY OUTPUT:
input=5
----#
---#-#
--#---#
-#-----#
Please explain how to do??
There are a few things missing and to be improved in your code:
There's no need to make a loop to print the same character again and again: on python you can use the product to repeat the character an x number of times. For example: "-" * 3 == "---"
They way you calculate the hyphens in the middle is fine, but you need to do it twice and add an "#" in between.
You can build the strings part by part first and then print the whole line, avoiding having to print an empty line in the end of the loop.
Personally, since the first line is going to have one "#" and not three, I prefer to calculate it and print it separately.
With these improvements, a solution to your problem could be:
row = int(input())
print("-" * (row - 1) * 2 + "#")
for i in range(row - 2, 0, -1):
left_hyphens = "-" * i * 2
mid_hyphens = "-" * (1 + 2 * (row - 2 - i))
print(left_hyphens + "#" + mid_hyphens + "#" + mid_hyphens + "#")
row = int(input())
for i in range(1, row):
for j in range(1,2*(row-i)+1):
print("-", end="")
for j in range(1, 4*i):
if j==1 or j==2*i-1 or j==4*i-3:
print("#", end="")
elif j<=4*i-3:
print("-", end="")
print()
Related
for i in range(n):
for j in range(n):
z = i * n + j
if z < 10:
print(f" {z}", end = " ")
else:
print(f"{z}" , end = " ")
print()
I have tried the end= " " method but I keep getting the whitespace error, I am a beginner thus I am unable to implement other methods to address the same. The image embedded below has the desired output
[The output required ][1]
[1]: https://i.stack.imgur.com/StAVT.png
This approach creates the full matrix as a string s first and then only prints once in the end:
s = ''
for i in range(n):
for j in range(n):
z = i * n + j
s += ' '
if z < 10:
s += ' '
s += str(z)
s += '\n'
print(s)
Why does my code not work in the 2nd iteration? I want to give input in row = 3 or 5 or x, and then I expect it to produce the following output pattern:
#When row = 3
1|. . #
2|. ##
3|###
Please note that the number of "." will be opposite to the number of "#"
The i loop prints a new line, the j loop prints "#", and the k loop prints "."
row = 3
for i in range(1, row + 1):
for j in range(1, i + 1):
for k in range(1, (row - i) + 1):
print(".", end= "")
print("#", end="")
print("")
Your k loop runs too many times to do what you want after the first iteration.
However I think this does what you want, hope it helps:
row = 3
n_hash = 0
n_dots = row
for i in range(row):
print(f'{i+1}|{n_dots * "."}{n_hash * "#"}')
n_dots -= 1
n_hash += 1
Using just indexing and loops, how do you swap the positions of adjacent characters in a string, while making an exception for spaces?
I/P: HELLO WORLD
O/P: EHLLO OWLRD
This is the code I wrote:
s1=''
for j in range(0,len(s)-1,2):
if(skip==1):
print()
skip=0
elif(s[j].isspace()==False and s[j+1].isspace()==False):
s1=s1+s[j+1]+s[j]
elif(s[j].isspace()==False and s[j+1].isspace()==True):
s1=s1+s[j]+" "
elif(s[j].isspace()==True and s[j+1].isspace()==False and s[j+2].isspace()==False):
s1=s1+" "+s[j+2]+s[j+1]
skip=1
elif(s[j].isspace()==True and s[j+1].isspace()==False and s[j+2].isspace()==True):
s1=s1+" "+s[j+1]
print("new string is",s1)
What exactly am I doing wrong here?
inp = "HELLO WORLD"
expected = "EHLLO OWLRD"
for i in range(0, len(inp), 2): # iterate in steps of two
# check to make sure that we are not at end of string and charaters are not spaces
if i+1 < len(inp) and inp[i] != " " and inp[i+1] != " ":
# now do the string replacing
temp1 = inp[i]
temp2 = inp[i + 1]
inp = inp[:i] + temp2 + temp1 + inp[i+2:]
# output to indicate the new string is what we expect
print(inp)
print(expected)
print(inp == expected)
Another solution:
s = "HELLO WORLD"
out = []
for w in map(list, s.split(" ")):
for i in range(1, len(w), 2):
w[i], w[i - 1] = w[i - 1], w[i]
out.append("".join(w))
print(" ".join(out))
Prints:
EHLLO OWLRD
Have a school project that I just can't seem to crack. I need to create a python multiplication table using for-loops and nested for loops. I have the code to create the table but the problem is that I have to copy the exact output that my teacher got when he made the program. His output:Output of the program.
My current code looks like this:
n = 12
print("*\t|", end = "\t")
for i in range(1, 13):
print(i, end = "\t")
print()
for i in range(1, 112):
print("=", end = "")
print()
for i in range(1, 13, 1):
print(i, "\t|")
for row in range(1, n + 1):
for col in range(1, n+1):
print(row * col, end = "\t")
print()
Any help would be much appreciated (sorry for any bad formatting this is my first post!)
You need to merge the code for the i loop and the row loop:
for row in range(1, n + 1):
print(row, "\t|", end = "\t")
for col in range(1, n+1):
print(row * col, end = "\t")
print()
Also, you might replace your 13 everywhere with n+1 for consistency, if you haven't already done that.
Below is the corrected code:
n = 12
print("*\t|", end = "\t")
for i in range(1, 13):
print(i, end = "\t")
print()
for i in range(1, 55):
print("=", end = "")
print()
for i in range(1, n+1, 1):
print(i, end="\t|\t")
for col in range(1, 13):
print(i * col, end="\t")
print()
for i in range(1, 13):
print("*", i, ":", end=" ")
for j in range(1, 13):
print("{:2d}".format(i * j), end=" ")
[![enter image description here][1]][1]print()
I want to create a function that takes 2 parameter, and prints the multiplication table for this number in a nice format where rows are separated by lines. This is the target:
target design
I have tried, but have no idea where to integrate the "--------" string. Any ideas?
def multi_table(x,y):
for row in range(1, x+1):
for col in range(1, y+1):
num = row * col
if num < 10: blank = ' '
else:
if num < 100: blank = ' '
print(blank, num, end = '')
print()
multi_table(4,5)
You need to add the print statement between the row and column loop. You also need to ensure that you end the print statement with a new line character \n. Refer below.
def multi_table(x,y):
for row in range(1, x+1):
print("---------------------\n")
for col in range(1, y+1):
num = row * col
if num < 10: blank = ' '
else:
if num < 100: blank = ' '
print(blank, num, end = '')
print()
multi_table(4,5)
The print() is used to go to the next line, and that's where you want to add the "---------------". So change the print() to print('\n------------------------\n'). \n indicates to go to the next line.
To compensate for y, you can use the following,
also, you can simplify the formatting with the format string method:
def multi_table(x,y):
for row in range(1, x+1):
print('----' * y)
for col in range(1, y+1):
num = row * col
print('{:4}'.format(num), end = '')
print()