因此,我制作了一个collatz计算器(如果你不知道它是什么,请查阅),我想记录达到1所需的步骤,然后将数据放入列表中。当一个数字超过2位时,它会在列表中将这些数字分成两个不同的点
这是密码
steps = 0
stepsList = []
numList = []
listCount = 0
while True:
numIn = int(input('enter number: '))
while True:
if numIn % 2 == 0:
numIn = numIn / 2
else:
numIn = (3 * numIn) + 1
print(numIn)
steps += 1
if numIn == 1:
numIn += 1
print('~~~')
print(str(steps) + ' steps')
stepsList.extend(str(steps))
print(stepsList)
break
假设我输入数字27,而不是将其存储为[111],它将其存储为[1,1,1]
增加了一些清洁和清洁;更好的ctrl-c处理:)
我觉得不错
只需替换以下行:
借
如果您需要每个不同测试编号的步数,而不是累积计数器,那么您也应该在外部循环开始时重置
steps
计数器相关问题 更多 >
编程相关推荐