import sys
def collatz(num):
if num % 2==0:
print('the num:',num//2)
return num//2
else:
print('the num:',3*num+1)
return(3*num+1)
'''user输入,判断是否为整数'''
num=input('please input a int number:')
'''user输入不是整数时提醒,并退出程序'''
try:
num=float(num)
except ValueError:
print('please input a integer')
sys.exit()
else:
if num==float(int(num)):
print('I am int')
num=int(num)
'''通过循环来实现函数collatz()'''
number=collatz(num)
while number!=1:
number=collatz(number)
try/except/else
最新推荐文章于 2021-03-07 16:53:58 发布