实践项目:collatz序列
def collatz(number):
if number % 2 == 0: #偶数除以2,余数为0
return number//2
elif number % 2 == 1: #奇数除以2,余数为1
return 3*number+1
while True:
try:
number = int(input(‘请输入一个整数:’))
except ValueError:
print(‘请输入一个整数:’)
continue
print(collatz(number))
if collatz(number) != 1:
continue
else:
break