Python计算器不带分数计算

2024-04-20 16:10:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个脚本,它将返回所有的素数,直到您输入的数字为止,问题是如果您问python它是17/2多少,它会回答8,也会回答27/2,它会回答13,我如何修复它?你知道吗

我试过float()但没用。你知道吗

编辑:我写的剧本,直到现在:

array=[2,3,5,7]
num=int(raw_input("Please enter a number higher then 8:    ex:12\n")) 
for i in range(8,num): 
    b=float(i)
    if b%2.0 and b%3.0 and b%4.0 and b%5.0 and b%6.0 and b%7.0 and b&8.0 and b%9.0!=0:    
        array.append(b)
        print array

Tags: and脚本编辑numberinputraw数字float
3条回答

如果要使用整数,可以使用:

from __future__ import division
a = 4
b = 6
c = a / b
print c

输出:

0.66666666666666663

我想你想要这个

num = 25

primeList=[]
for val in range(2,num):
    if not any(val%i==0 for i in primeList):
        primeList.append(val)

print primeList

对于num=25,它输出:(python 2.7.6)

[2, 3, 5, 7, 11, 13, 17, 19, 23]

对于num=100,它输出:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,  
 67, 71, 73, 79, 83, 89, 97]

试试这个

17 / 2.0 or 

17.0 / 2

相关问题 更多 >