glw
glw

Reputation: 5

Python(3), Loop over tuple and use string format to display

My homework assignment is to Loop over ((1, 1), (2, 2), (12, 13), (4, 4)) and use string formatting to display this tuple as:

1 = 1 x 1  
4 = 2 x 2
156 = 12 x 13 
16 = 4 x 4

while also preserving the spacing.

What I have so far:

d = ((1,1), (2,2), (12,13), (4,4))
for a, b in d:
    print("{0} = {1}".format(a* b, d))

Which gives me:

1 = ((1, 1), (2, 2), (12, 13), (4, 4))
4 = ((1, 1), (2, 2), (12, 13), (4, 4))
156 = ((1, 1), (2, 2), (12, 13), (4, 4))
16 = ((1, 1), (2, 2), (12, 13), (4, 4))

So it seems to me that I am getting close. But I have run out of ideas to get the right side of the equation into the correct format. Any ideas would be greatly appreciated.

Upvotes: 0

Views: 1986

Answers (2)

sth
sth

Reputation: 229914

On the right side of the equation you want to print the content of variable a, then the character x and then the content of variable b.

Upvotes: 1

psantiago
psantiago

Reputation: 714

d = ((1,1), (2,2), (12,13), (4,4))
for a, b in d:
    print("{0} = {1} x {2}".format(a* b, a, b))

Upvotes: 3

Related Questions