First off, let me explain what I'm attempting to do.
I'm wanting to print a quadrilateral(square shape) using strings from a list. Like so:
John
o h
h o
nhoJ
I get the printed results I'm wanting except my for loop is printing more than needed. I just need to skip a few loop iterations depending on the length of the actual string.
This is where I need help, this is inside the "quad_text" function:
for i in norm_list[1:-1]:
for j in reve_list[1:-1]:
print(f"{i}{spaces}{j}")
My outputted results right now:
---
John
o h
o o
h h
h o
nhoJ
---
Joe
o o
eoJ
---
David
a i
a v
a a
v i
v v
v a
i i
i v
i a
divaD
Full Code
#Global Variables"
normal_list = ["John", "Joe", "David"]
#normal_list = ["Hey there buddy!"]
def initialization():
rev_list = string_reverser()
combined_lists = zip(normal_list, rev_list)
quad_text(list(combined_lists))
def string_reverser():
reversed_list = []
for i in normal_list:
reversed_list.append(i[::-1])
return(reversed_list)
def quad_text(combined_lists):
for (norm_list, reve_list) in combined_lists:
print("---")
print(norm_list)
text_len = len(norm_list)
spaces = (text_len - 2) *(" ")
for i in norm_list[1:-1]:
for j in reve_list[1:-1]: # <--- Problem Area
print(f"{i}{spaces}{j}")
print(reve_list)
initialization()
Don't use nested loops, that creates a cross product between the forward and reverse lists. Use
zip()
to loop over them together.