鱼C论坛

 找回密码
 立即注册
查看: 3465|回复: 6

求一个属的分解因数,我看了好就没看懂哪里错了,谁能帮一下

[复制链接]
发表于 2020-2-29 14:01:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. def dec(shu):
  2.     while int(shu)==False :
  3.         shu = input('请输入正确的数字:')
  4.     shu = int(shu)
  5.     a = shu
  6.     if shu == 1:
  7.         print('1=1')
  8.     if shu == 2:
  9.         print('2 = 1 * 2')
  10.     if shu>2:
  11.         print('%d = ' % shu,end='')
  12.         while True:
  13.             for i in range(2,shu+1):
  14.                 if shu%i==0:
  15.                     if i==shu:
  16.                         print('%d' % i,end='')
  17.                     else :
  18.                         print('%d*' % i,end='')
  19.                         shu = shu/i
  20.                     break
  21.             if i==a:
  22.                 print('*1')
  23.             if i==shu:
  24.                 break
复制代码

我输入的是素数的话就没错,但是输入不是素数就会报错比如下面的
>>> dec(6)
6 = 2*Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    dec(6)
  File "E:\PY文件\求一个属的分解因数.py", line 13, in dec
    for i in range(2,shu+1):
TypeError: 'float' object cannot be interpreted as an integer
但是这个哪来的float啊我都不知道,我看了其他人答案,像我这样写的很多啊,没搞懂我哪错了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-29 14:37:04 | 显示全部楼层
19行,要用地板除
代码:
  1. def dec(shu):
  2.     while int(shu)==False :
  3.         shu = int(input('请输入正确的数字:'))
  4.     a = shu
  5.     if shu == 1:
  6.         print('1=1')
  7.     if shu == 2:
  8.         print('2 = 1 * 2')
  9.     if shu>2:
  10.         print('%d = ' % shu,end='')
  11.         while True:
  12.             for i in range(2,shu+1):
  13.                 if shu%i==0:
  14.                     if i==shu:
  15.                         print('%d' % i,end='')
  16.                     else:
  17.                         print('%d*' % i,end='')
  18.                         shu = shu//i # 地板除法
  19.                     break
  20.             if i==a:
  21.                 print('*1')
  22.             if i==shu:
  23.                 break
复制代码

如果有帮助,请设最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-29 14:40:47 | 显示全部楼层
shu =shu/i是float浮点数呐
使用int或round或floor或ceil进行取整
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-29 15:14:42 | 显示全部楼层
本帖最后由 major_lyu 于 2020-2-29 15:18 编辑
  1. def dec():
  2.     shu = input('请输入正确的数字:')
  3.     while int(shu) <= 0:
  4.         shu = input('请输入正确的数字:')
  5.     shu = int(shu)
  6.     a = shu
  7.     if shu == 1:
  8.         print('1=1')
  9.     if shu >= 2:
  10.         print('%d =1 * ' % shu, end='')

  11.         # for i in range(2, shu + 1):
  12.     while shu > 1:
  13.         for i in range(2, shu+1):
  14.             if shu % i == 0:
  15.                 if i == shu:
  16.                     print('%d' % i, end='')
  17.                 else:
  18.                     print('%d * ' % i, end='')

  19.                 # shu = shu / i  # shu / i 式计算shu除以i,当然是float了
  20.                  shu = shu // i    # 商取整是 // 运算
  21.                 break


  22. def main():
  23.     dec()


  24. if __name__ == '__main__':
  25.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-29 19:07:36 | 显示全部楼层
qiuyouzhi 发表于 2020-2-29 14:37
19行,要用地板除
代码:

但是我前提有那个余数是0啊,这样的两个数相除出来一定是整数啊,这样系统也会把它们两的商归为浮点型吗
而且用地板除就是说那个商自动变为整型了吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-29 19:08:45 | 显示全部楼层
来mua一口 发表于 2020-2-29 19:07
但是我前提有那个余数是0啊,这样的两个数相除出来一定是整数啊,这样系统也会把它们两的商归为浮点型吗
...

地板除的结果除了0没有小数部分
你把相除的结果print试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-29 19:32:54 | 显示全部楼层
来mua一口 发表于 2020-2-29 19:07
但是我前提有那个余数是0啊,这样的两个数相除出来一定是整数啊,这样系统也会把它们两的商归为浮点型吗
...

是的,而且除法计算的结果永远是浮点数,不管是否能整除。例如:

  1. >>> 8 / 4
  2. 2.0
  3. >>> 12 / 3
  4. 4.0
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-23 01:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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