[英]My python program is not working properly, i'm not exactly sure why
Firstly, I'm a beginner and I don't speak English very well, so any questions about what I'm trying to say are welcome.首先,我是初学者,英语说得不是很好,所以欢迎大家对我想说的内容提出任何问题。 I was making a python program that received two numbers, and the program showed the prime numbers between them.
我正在制作一个接收两个数字的 python 程序,该程序显示了它们之间的质数。
The algorithm takes the numbers, turns them into a list, then divides each number in that list by each of its predecessors.该算法获取这些数字,将它们变成一个列表,然后将该列表中的每个数字除以它的每个前身。 Then each integer division is passed to another list to see which of the numbers passed to the other list are primes.
然后将每个integer除法传递给另一个列表,看看传递给另一个列表的数字中哪些是质数。
Once passed to the second list, the prime numbers are those that are not repeated in that list, that is, when removing all the repeated values, I have a list of primes, but in some values the function simply does not work.一旦传递到第二个列表,素数就是那些在该列表中没有重复的数字,也就是说,当删除所有重复的值时,我有一个素数列表,但在某些值中 function 根本不起作用。 I would like to understand why.
我想明白为什么。 Thanks in advance!
提前致谢!
Follow the code below:请遵循以下代码:
def Find_Primes(smaller_num, bigger_num):
array = list(range(smaller_num, (bigger_num + 1)))
array2 = []
for i in array:
for j in range(1, i):
if i % j == 0:
array2.append(i)
for num in array2:
if array2.count(num) > 1:
while num in arrayt2:
arrayt2.remove(num)
print(array2)
smaller_number = int(input('Text the smaller number of interval: '))
bigger_number = int(input('Text the other number of interval: '))
Find_Primes(smaller_number, bigger_number)
Your are adding i
multplied by j
numbers to array2 instead if (probably?) j
numbers.如果(可能?)
j
个数字,则您将i
乘以j
个数字添加到 array2。 Look at the output.查看 output。
def Find_Primes(smaller_num, bigger_num):
array = list(range(smaller_num, (bigger_num + 1)))
array2 = []
for i in array:
#numbers from input
for j in range(1, i):
if i % j == 0:
array2.append(i)
print(array2)
print("\n")
for num in array2:
if array2.count(num) > 1:
while num in array2:
array2.remove(num)
print(array2)
smaller_number = 1
bigger_number = 15
Find_Primes(smaller_number, bigger_number)
output: output:
[2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 8, 8, 9, 9, 10, 10, 10, 11, 12, 12, 12, 12, 12, 13, 14, 14, 14, 15, 15, 15]
[2, 3, 5, 7, 11, 13, 15, 15, 15]
def find_primes(smaller_num, bigger_num):
# empty list to hold list of prime numbers
lst_of_primes = []
# loop to go through numbers in the range
for number in range(smaller_num, bigger_num):
# variable to hold the count of factors
factors = 0
# loop to test possible factors for the number
for test_num in range(2, number):
# if integer division leaves no remainder
if number % test_num == 0:
# increment factors variable
factors += 1
# number has no factors
if factors == 0:
# add it to the list of prime numbers
lst_of_primes.append(number)
# output the final list
print(lst_of_primes)
smaller_number = int(input('Enter the smaller number of interval: '))
bigger_number = int(input('Enter the larger number of interval: '))
find_primes(smaller_number, bigger_number)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.