Python 计算器计算没有分数
Python calculator calculate without fraction
我正在尝试编写一个脚本,它将 return 你所有的质数直到你输入的数字,问题是如果你问 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
试试这个
17 / 2.0 or
17.0 / 2
如果你想使用整数,你可以使用:
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]
我正在尝试编写一个脚本,它将 return 你所有的质数直到你输入的数字,问题是如果你问 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
试试这个
17 / 2.0 or
17.0 / 2
如果你想使用整数,你可以使用:
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]