【问题标题】:Python - For Loop That Prints Each Number And Its Square on a Separate LinePython - 在单独的行上打印每个数字及其正方形的 For 循环
【发布时间】:2017-03-19 09:53:25
【问题描述】:

我有一个数字列表:

nums = [12, 10, 32, 3, 66, 17, 42, 99, 20]

我的第一个任务是在新行上打印每个数字,如下所示:

12
10
32
3
66
17
42
99
20

我可以通过创建一个打印列表中整数的 for 循环来做到这一点:

nums = [12, 10, 32, 3, 66, 17, 42, 99, 20]
for i in nums:
    print(i)

现在我需要编写另一个 for 循环,在新行上打印每个数字及其正方形,如下所示:

The square of 12 is 144
The square of 10 is 100 
The square of 32 is 1024
The square of 3 is 9
The square of 66 is 4356
The square of 17 is 289
The square of 42 is 1764
The square of 99 is 9801
The square of 20 is 40

到目前为止,我实际上已经能够打印平方值,但不能单独将它们放在单独的行上。我还需要去掉值周围的括号。

我的尝试:

nums = [12, 10, 32, 3, 66, 17, 42, 99, 20]
for i in nums:
    print(i)

squared = [ ]

for i in nums:
    squared.append(i * i)
    print("The square of", i, "is", squared)

导致:

12
10
32
3
66
17
42
99
20
The square of 12 is [144]
The square of 10 is [144, 100]
The square of 32 is [144, 100, 1024]
The square of 3 is [144, 100, 1024, 9]
The square of 66 is [144, 100, 1024, 9, 4356]
The square of 17 is [144, 100, 1024, 9, 4356, 289]
The square of 42 is [144, 100, 1024, 9, 4356, 289, 1764]
The square of 99 is [144, 100, 1024, 9, 4356, 289, 1764, 9801]
The square of 20 is [144, 100, 1024, 9, 4356, 289, 1764, 9801, 400]

我需要去掉括号,每行只需要包含一个平方值(对应于左边的整数)。我该怎么做?

【问题讨论】:

  • 为什么要建立一个列表?为什么不在您创建的 first 循环中使用print() 来打印i * i 结果?
  • 遵循第一个示例中的相同方法,并在循环中打印:print("The square of", i, "is", i * i)。您无需在新列表中收集值。
  • 我不知道为什么我没想到 - 是的,它按预期工作,谢谢!

标签: python list for-loop printing integer


【解决方案1】:

我不确定您是否真的想要跟踪方块,或者这只是您打印它们的一部分。

假设您确实想要跟踪方块,那么小的变化是:

https://repl.it/GLmw

nums = [12, 10, 32, 3, 66, 17, 42, 99, 20]
for i in nums:
    print(i)

squared = [ ]

for i in nums:
    sqr = i * i
    squared.append(sqr)
    print("The square of {} is {}".format(i, sqr))

这使您无需从数组中引用当前方块即可使用它。

如果您确实是要从数组中引用平方值,则可以使用负索引从数组末尾获取:

https://repl.it/GLms

nums = [12, 10, 32, 3, 66, 17, 42, 99, 20]
for i in nums:
    print(i)

squared = [ ]

for i in nums:
    squared.append(i*i)
    print("The square of {} is {}".format(i, squared[-1]))

【讨论】:

  • 使用负索引为每个平方结果提供 400,但您的第一个代码块可以满足我的要求。除了,有没有办法打印没有逗号的结果?我目前得到 ('The square of', 12, 'is', 144) 如果我尝试这样做: print("The square of" + i + "is" + sqr) 我收到错误消息:TypeError :不能连接'str'和'int'对象知道如何解决这个问题吗? pythontutor.com/visualize.html#mode=edit
  • @HappyHands31 对此我深表歉意。我仍然部分处于 Python 2 的状态。我已经将代码更新到 Python 3,修复了第二个块中的一个错误,并添加了一些在线演示。
  • 感谢它现在运行良好。你介意解释一下吗?当你说 print("The square of {} is {}".format(i, sqr)) 时,看起来你在做字符串替换?而你用 {} 代替的值是“i”和“sqr”......所以“squared.append(sqr)”行是确保我们在当前方格上的原因阵列?这样,每当您在打印语句中引用“sqr”时,它就知道每次都打印出下一个平方值?对吗?
  • @HappyHands31 squared 仅用于存储未来未编写的代码。 sqr 跟踪当前平方值,然后将其用于追加到数组并组装成字符串。每次循环使用i 的当前值,sqr 都会重新初始化。 .format() is described here.
【解决方案2】:
nums = (12, 10, 32, 3, 66, 17, 42, 99, 20)

for i in nums:
    print(i)

for i in nums:
    print("the square of",i,"is", i**2)

【讨论】:

    【解决方案3】:
    nums = [12, 10, 32, 3, 66, 17, 42, 99, 20]
    for i in nums:
        print(i**2)
    

    【讨论】:

    • 欢迎来到 StackOverflow!您应该描述您发布的任何代码,以便提问者和社区能够更好地理解它,无论它多么小。
    猜你喜欢
    • 2021-03-06
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多