下面的程序要求用戶輸入「測試用例的數量」,然後輸入數字以對其進行操作。最後,我想打印循環中每個操作的結果。如何在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' –
請你能把它放在我的代碼,並添加它,請我不知道在哪裏我就會把它和感謝你 –