下面的程序要求用户输入“测试用例的数量”,然后输入数字以对其进行操作。最后,我想打印循环中每个操作的结果。如何在python中一次打印每个循环的所有结果
这是代码:
test_case = int(raw_input()) # User enter here the number of test case
for x in range(test_case):
n = int(raw_input())
while n ! = 1: # this is the operation
print n,1, #
if n % 2 == 0:
n = n//2
else:
n = n*3+1
下面是输出,如果我在测试情况下输入“2”,并在每种情况下2号。对于实施例22和64将是这样的:
2
22 # the first number of the test case
22 1 11 1 34 1 17 1 52 1 26 1 13 1 40 1 20 1 10 1 5 1 16 1 8 1 4 1 2 1 # it prints the result immediately
64 # second test case
64 1 32 1 16 1 8 1 4 1 2 1 # it prints it immediately as the first
下面是所期望的输出:
2
22
64
用户后输出进入测试案例和测试用例的所有数目是:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
64 32 16 8 4 2 1
我该如何解决这个问题?
注:我试图将结果保存在列表中并打印出来,但它将所有结果打印在一行中。
'对于结果的结果:打印result' –
请你能把它放在我的代码,并添加它,请我不知道在哪里我就会把它和感谢你 –