English 中文(简体)
在我尝试制定法典时,我如何解决我的错误?
原标题:How can I solve my error when I try to run code?
def is_prime(n):
    # Write your function code here...
    if 2 <= n and n < 1000 :
        for i in range(2, n):
            if n % i == 0:
              return False
            else:
              return True
        
        
print(is_prime(975))

因此,(is_prime(975)) 是初步的,n = 2None。 我需要回答975是假的, = 2是真实的。

问题回答

有些人认为:在使用<代码>return<>/code>时,他们认为,即使在恢复价值之后, lo仍会变。 但这不是这样。 <代码>return 说明给出了该休息时间的价值和突破。 例如, 根据您的代码,在指定职能<代码>(_prime(15))时,产出应为。 真正。 在蒸发期间,它检查了2米(2,n)(e)中的第一个数值。

第2号将15个区分开;平衡不是零。 因此返回 真实; 停泊。 它对距离(2,15)的其他数值不作检查。

但是,为了确定某一数目是否是首要的,应当确保该数字不会因特定范围的所有数字而分出。 因此,可以用其他方式撰写。

def is_prime(n):
    result = True
  
    for i in range(2, n):
        if n % i == 0:
            result = False; break
    return result
            

r = is_prime(15)
print(r) #Output: False

r = is_prime(17)
print(r) #Output: True

此处称为“条码” 真正。 胎面((2,n)中正在使用。 现在即使有一例,也会出现分歧,但返回代码。 如果在范围的所有数字中都看不出任何数字,就会退回违约值<代码>。 真实

页: 1 无论是否是主要数字,对任何大于或等于2,但小于1,0的无。 这是因为,这一职能只是确定,在2至1之间,任何数字都不能区分,如果是真的,则会返回。

你们必须改变你的职能逻辑,以便解决问题,正确发现某个人是否是首要的。 这是经过更新的版本,从2到2平方_root of n + 1

def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

print(is_prime(975))  # This should print False
print(is_prime(2))    # This should print True

第一部分

for i in range(2,n):    LINE-1
    if(n%i==0):         LINE-2
         return False   LINE-3
    else:             
         return True

在LINE-2,将改为和NOT

休息时间将从2开始,将一直到(n-1),而按比(例如:2 % 975 = 0)多出一分,总会给零,因为你为什么要为975人“False”服务。

For 2 the for loop doesn t execute as the condition (staring_index < ending_index) is False so it goes into the else condition to print "True"





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签