Python 结果作为一个列表
嗨,我写了一个代码,如果你输入一个数字,它会给你一些数字Python 结果作为一个列表,python,python-3.x,Python,Python 3.x,嗨,我写了一个代码,如果你输入一个数字,它会给你一些数字 n= int(input("Enter the beginning number: ")) i=0 print(n) while n > 1: if n%2 == 0: n = n/2 print(n) else: n = 3*n+1 print(n) i += 1; 但是现在我怎么才能把它加起来看起来像[…] 我已经尝试添加了
n= int(input("Enter the beginning number: "))
i=0
print(n)
while n > 1:
if n%2 == 0:
n = n/2
print(n)
else:
n = 3*n+1
print(n)
i += 1;
但是现在我怎么才能把它加起来看起来像[…]
我已经尝试添加了
theList = []
....
....
....
if..
...
theList.append(i) # or theList.append(n) its the same
那么,如何将数字添加到您的列表中呢?然后,当您想查看时,请按如下方式打印您的列表:
print ','.join(map(str,theList)) #thanks to adam smith for catching that
['a','b','c','d']
'a,b,c,d'
它列出了如下列表:
print ','.join(map(str,theList)) #thanks to adam smith for catching that
['a','b','c','d']
'a,b,c,d'
打印如下:
print ','.join(map(str,theList)) #thanks to adam smith for catching that
['a','b','c','d']
'a,b,c,d'
你试着在名单上加上,但发生了什么?这是正确的想法,但我们不知道什么对你不起作用我得到的结果是1 2 3 4 5一个比另一个低不象[1,2,3,4,5]可能代替了所有的
…
,您可以显示试图附加到列表的实际代码。?祝Collatz猜想好运:-)同上,这将引发类型错误
,因为OP将输入转换为整数。您需要执行打印','。join(map(str,theList))
然后它将工作:)如果输入10,我将得到五个空行?