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
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)
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()
This is the exercise I need to complete:
Using nested loops, write some code that outputs the following:
##########
**********
##########
**********
##########
Following is all the code I have so far. I assume I need to combine these two loop functions to one but I'm struggling with it. I'm early on in my first class and any help with this would be appreciated!
for a in range (0, 5, 2):
for b in range(10):
print("#", end = "")
print("")
for a in range (1, 5, 2):
for b in range(10):
print("*", end = "")
print("")
Since no input is specified, only a fixed output:
for _ in '_':
for _ in '_':
print('''##########
**********
##########
**********
##########''')
And yes, if that was my homework exercise, I'd absolutely submit this.
There are several ways to do it. First you should check how many lines you need to output. 5, so you need a loop doing something 5 times.
for i in range(5):
Now you need 2 loops to paste print the 2 line patterns (you already have them in your code)
for b in range(10):
print("#", end = "")
print("")
and
for b in range(10):
print("*", end = "")
print("")
If you need to alternate between 2 values it's mostly the best to use the Modulo Operator.
So you can just switch between the 2 loops by % 2.
for i in range(5):
if i % 2 == 0:
for b in range(10):
print("#", end = "")
print("")
else:
for b in range(10):
print("*", end = "")
print("")
I would suggest to simply base the symbol on the row index, even or odd
nb_lines = 5
line_size = 10
for i in range(nb_lines):
for _ in range(line_size):
if i % 2 == 0:
print("#", end="")
else:
print("*", end="")
print()
But no need of nested loops
nb_lines = 5
line_size = 10
for i in range(nb_lines):
if i % 2 == 0:
print("#" * line_size)
else:
print("*" * line_size)
def printStripes(row_length, number_of_rows):
for _ in '_':
print( "".join(
[ "\n" if n%(row_length+1)==row_length else
"#" if (n//(row_length+1))%2==0 else
"*"
for n in range(number_of_rows*(row_length+1))
]
),end="")
printStripes(row_length=5,number_of_rows=8)
But don't tell your teacher that you got if from stackoverflow. (Thanks Kelly, for how to deal with the nested loop constraint.)
You can use this to get the result that you want
for i in range(0,5):
print("*"*10)
print("#"*10)
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()