求一个数的最大公约数

来源:互联网 发布:淘宝商品降权多久恢复 编辑:程序博客网 时间:2024/05/17 08:54
# -*- 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 也是一样。

0 0
原创粉丝点击