New course! Every coder should learn Generative AI!
Try a free lesson0
Printing "W" Pattern in python, need help to find bug
number of spaces increase with every loop, So our letter "W" is not drawing perfectly, please check and help . for i in range(1,10): for j in range(1,i): print(' ',end='') for k in range(1,2): print('x') if i in range(1,5): print('') else: for m in range(1,2): for l in range(20,i,-1): print(' ',end='') print('x') if i in range(1,5): print('') else: for n in range(-11,i): print(' ',end='') for o in range(1,2): print('x') for p in range(30,i,-1): print(' ',end='') for q in range(1,2): print('x')
1 Answer
0
Here you go!
for i in range(10):
if i in range(5):
print(' '*i+"x",end="")
else:
print(' '*i+"x", end="")
if i in range(5,10):
if i != 9:
print(' '*(-2*i + 17)+"x",end="")
if i != 5:
print(' '*(2*i - 11)+"x",end="")
if i in range(5):
print(' '*(-2*i+25)+"x",end="")
elif i in range(5,9):
print(' '*(-2*i + 17)+"x",end="")
print()
● range(1,2) just loops once. The same will work if you remove it.
● every print() you go must end with "". Then at the end of the code(inside the for loop), you must add print() so your W will attach to each other.
● you can multiply string by its number. like example, 'x' * 3 results xxx.
● You actually need math for this. By computing the linear equation and use it for the code.