简体   繁体   English

我的 python 程序运行不正常,我不确定为什么

[英]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.

相关问题 Python - 我不确定为什么我定义的这个函数不起作用。 对编程很新鲜 - Python - I'm not sure why this function I'm defining isn't working. Very fresh to programming 我的代码的一部分不起作用,我不知道为什么(python) - A section of my code is not working and I am not sure why (python) 我不确定win32gui.EnumWindows是否正常工作 - I'm not sure win32gui.EnumWindows is working properly 我的 line.split 函数没有按预期工作,我不知道为什么 - My line.split function is not working as intended and I'm not sure why 运行此程序时出现属性错误。 我正在使用 python 中的 kivy 模块,但不确定如何克服这个问题 - I get an attribute error when I run this program. I'm working with kivy module in python and am not sure of how to overcome this Python:我不确定为什么我的 discord 机器人代码会出现此错误 - Python: I'm not sure why my discord bot code is giving this error 我的 python output 缺少 1 个单词,我不确定为什么(从文件中读取) - My python output is missing 1 word and I'm not sure why(reading from file) 我的IF语句出现语法错误,不确定为什么吗? - I'm getting a syntax error on my IF statement, not sure why? 我的 window 没有任何反应,我不知道为什么 - nothing has come on my window and i'm not sure why 不知道为什么我卡在 Python 卡住递归循环 - Not sure why I'm stuck in a Python stuck recursion loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM