Home > other >  O solve the python
O solve the python

Time:12-18

Def collatz (number) :
If number % 2==0:

Return the number//2
The else:
Return 3 * number + 1

Try:
Input_number=int (input (" please enter an integer: "))
While True:
Input_number=collatz (input_number)
Print (collatz (input_number))
If input_number==1:
Break
Except:
Print (' error! Support only integer input! ')


# question: why output will more than 4?

Please enter an integer: 8
2
1
4

CodePudding user response:

Down in the first round input_number passed from 8 to 4, type 2; The second round down input_number passed from 4 to 2, print 1; The third round down input_number passed from 2 to 1, to print 4,
And then jump out of the loop,
  • Related