Python(3)、循环元组并使用字符串格式显示
我的作业是循环 ((1, 1), (2, 2), (12, 13), (4, 4)) 并使用字符串格式将此元组显示为:
1 = 1 x 1
4 = 2 x 2
156 = 12 x 13
16 = 4 x 4
,同时保留间距。
到目前为止我所拥有的:
d = ((1,1), (2,2), (12,13), (4,4))
for a, b in d:
print("{0} = {1}".format(a* b, d))
这给了我:
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))
所以在我看来,我已经接近了。但我已经没有办法将等式右边变成正确的格式。任何想法将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在等式右侧,您要打印变量
a
的内容,然后打印字符x
,然后打印变量b
的内容。On the right side of the equation you want to print the content of variable
a
, then the characterx
and then the content of variableb
.