[英](Python 3) Spent an hour but couldn't find the error
I am a beginner at learning python 3 and I am just writing basic programs. 我是学习python 3的初学者,我只是在编写基本程序。 I wrote this simple program which would take a number in and divide it by numbers starting from 1 to the square root of the number and find the remainders and add it to a list and print it.
我写了这个简单的程序,它将一个数字输入并除以从1到数字平方根的数字,然后找到余数并将其添加到列表中并打印出来。
import math
def prime_checker(num):
n=1
list_of_remainder=[]
while n == math.floor(num**0.5):
var=int(num % n)
list_of_remainder.append(var)
n += 1
return list_of_remainder
var=prime_checker(10)
print(var)
Please tell me what I did wrong. 请告诉我我做错了。 I would like to point out here that I did try to research a bit and find error but I couldn't and only then have I posted this question.
我想在这里指出,我确实做了一些研究并发现了错误,但是我不能,只有那时我才发布了这个问题。 The problem that I faced was that it printed out an empty list.
我面临的问题是它打印了一个空列表。
to start with, your while loop is not executed even once. 首先,您的while循环不会执行一次。 The condition for your while loop is
while n == math.floor(num**0.5):
The argument num you are passing to the function prime_checker is equal to 10. In this case your condition test is: while 1 == math.floor(10**0.5)
which is while 1 == 3
which is obviously not true and as a result the loop is not executed even once. while循环的条件为
while n == math.floor(num**0.5):
您要传递给函数prime_checker的参数num等于10。在这种情况下,您的条件测试为: while 1 == math.floor(10**0.5)
, while 1 == 3
显然是不正确的,因此循环甚至不会执行一次。
import math
def prime_checker(num):
list_of_remainder = []
number=num;
n=1
x=math.floor(number**0.5)
while n <= x:
v=int(number % n)
list_of_remainder.append(v)
n += 1
return list_of_remainder
var=prime_checker(10)
print(var)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.