我试着编写一个脚本,它将返回所有素数,直到你输入的数字为止,问题是如果你问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
答案 0 :(得分:2)
试试这个
17 / 2.0 or
17.0 / 2
答案 1 :(得分:2)
如果您想使用整数,可以使用:
from __future__ import division
a = 4
b = 6
c = a / b
print c
输出:
0.66666666666666663
答案 2 :(得分:0)
你想要这个我猜
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]