# -*- coding: utf-8 -*-
def showMaxFactor(number):
count=number//2
while count>1:
if number%count==0:
print("%d的最大公约数为%d" %(number,count))
break
count-=1
else:
print("%d是素数" %number)
try:
number=int(input("请输入一个正整数:"))
showMaxFactor(number)
except ValueError as reason:
print("请输入正整数,错误原因:"+str(reason))
注:这里else与while连用。
循环使用 else 语句
在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。