简体   繁体   English

Python计算器无分数计算

[英]Python calculator calculate without fraction

I'm trying to write a script that will return you all the prime numbers till the number that you entered, and the problem is that if you ask python how much it's 17/2 , it will answer 8, also with 27/2 it will answer 13, how do I fix it ? 我正在尝试编写一个脚本,该脚本将向您返回所有质数,直到您输入的数字为止,问题是,如果您问python 17/2多少,它将回答8,也27/2会回答13,我该如何解决?

I tried float() but it doesn't work. 我试过float()但是不起作用。

Edit: The script that I wrote, till now: 编辑:到目前为止,我编写的脚本:

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

try this 尝试这个

17 / 2.0 or 

17.0 / 2

If you want to use integers you can use: 如果要使用整数,可以使用:

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

Outputs: 输出:

0.66666666666666663

You want this I guess 我想你要这个

num = 25

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

print primeList

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

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

for num=100 , it outputs: 对于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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM