鱼C论坛

 找回密码
 立即注册
查看: 2625|回复: 4

python-34讲,求最大公约数的,报错,要怎么改??

[复制链接]
发表于 2014-8-18 22:42:40 | 显示全部楼层 |阅读模式
10鱼币
本帖最后由 欣欣celin 于 2014-8-20 22:02 编辑

def intInput(prompt = ''):
    while True:
        try:
            int(input(prompt))
            break
        except ValueError:
            print('出错,您输入的不是整数')

def showMaxFactor(num):
    count = num // 2
    while count > 1:
        if num % count == 0:
            print('%d最大公约数是%d' % (num, count))
            break
        count -= 1
    else:
        print('%d是素数:' % num)

num = intInput('请输入一个数: ')   
showMaxFactor(num)


>>>
请输入一个数: 9
Traceback (most recent call last):
  File "E:\python\34testing\elsewith.py", line 50, in <module>
    showMaxFactor(num)
  File "E:\python\34testing\elsewith.py", line 39, in showMaxFactor
    count = num // 2
TypeError: unsupported operand type(s) for //: 'NoneType' and 'int'

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-8-19 00:23:15 | 显示全部楼层
  1. def intInput(prompt = ''):
  2.     while True:
  3.         try:
  4.             int(input(prompt))
  5.             break
  6.         except ValueError:
  7.             print('出错,您输入的不是整数')

  8. def showMaxFactor(num):
  9.     count = num // 2
  10.     while count > 1:
  11.         if num % count == 0:
  12.             print('%d最大公约数是%d' % (num, count))
  13.             break
  14.         count -= 1
  15.     else:
  16.         print('%d是素数:' % num)

  17. num = int(input('请输入一个数: ') )#问题出在这里!
  18. showMaxFactor(num)
复制代码

  1. >>> ================================ RESTART ================================
  2. >>>
  3. 请输入一个数: 9
  4. 9最大公约数是3
  5. >>>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-8-19 02:39:27 | 显示全部楼层
我觉得上面那个函数应该和下面那个写一起。。。不然上面那个太鸡肋了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-8-19 02:52:13 | 显示全部楼层
  1. def showMaxFactor():
  2.     while True :
  3.         try:
  4.             num = int (input ('输入一个数字'))
  5.             break
  6.         except ValueError :
  7.             print ('请输入一个整数')
  8.     count = num // 2
  9.     while count > 1:
  10.         if num % count == 0:
  11.             print('%d最大公约数是%d' % (num, count))
  12.             break
  13.         count -= 1
  14.     else:
  15.         print('%d是素数:' % num)


  16. showMaxFactor()
复制代码

这样
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-3-16 17:03:58 | 显示全部楼层
def intInput(prompt = ''):
    while True:
        try:
            int(input(prompt))
            break
        except ValueError:
            print('出错,您输入的不是整数')
这一段一直看不大懂
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-15 08:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表