【发布时间】:2016-11-18 14:30:51
【问题描述】:
我正在尝试打印不带括号的列表。
我的代码是:
import math
bignumber = int(input("Enter a value: "))
listofsquares = []
for counter in range(bignumber):
squareroot = math.sqrt(counter)
if squareroot.is_integer():
square = squareroot*squareroot
listofsquares.append(int(square))
" ".join(str(listofsquares))
print(listofsquares)
我的输出是:
Enter a value: 100
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
我期待 .join() 去掉括号,但它只去掉了引号。这特别奇怪,因为它在之前使用它时已将它们删除。
【问题讨论】:
-
您确实需要将代码和输出放入您的问题本身。代码图像使任何人都无法复制和粘贴您的代码,以便他们可以运行它。另外,这样做的原因是因为您没有加入列表,而是加入了字符串。
-
我试过了,但是格式全乱了。它不接受新行或缩进。不过我会编辑它。
-
高亮显示代码并按 Ctrl + K。
-
" ".join(str(listofsquares))。你没有加入listofsquares。在将其转换为字符串后,您将加入listofsquares。所以它将listofsquares的字符串表示的每个字符都用空格连接起来。
标签: python python-3.x