Python(3)、循环元组并使用字符串格式显示

发布于 2024-12-08 10:26:30 字数 546 浏览 0 评论 0原文

我的作业是循环 ((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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很快妥协 2024-12-15 10:26:30
d = ((1,1), (2,2), (12,13), (4,4))
for a, b in d:
    print("{0} = {1} x {2}".format(a* b, a, b))
d = ((1,1), (2,2), (12,13), (4,4))
for a, b in d:
    print("{0} = {1} x {2}".format(a* b, a, b))
扛刀软妹 2024-12-15 10:26:30

在等式右侧,您要打印变量a 的内容,然后打印字符x,然后打印变量b 的内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文