3.11.1:
def collatz(number):if number%2 == 0:
print(str(number//2))
return str(number//2)
elif number%2 == 1:
print(str(3*number + 1))
return str(3*number + 1)
print('输入一个正整数:')
shu = int(input())
while True:
shu = int(collatz(shu))
if shu == 1:
break
3.11.2:
def collatz(number):
if number%2 == 0:
print(str(number//2))
return str(number//2)
elif number%2 == 1:
print(str(3*number + 1))
return str(3*number + 1)
print('输入一个整数:')
try:
shu = int(input())
except ValueError:
print('请输入一个整数!')
while True:
shu = int(collatz(shu))
if shu == 1:
break