简体   繁体   English

生成比列表中找到的数字大的质数

[英]Generating a prime number bigger than a number found in a list

What isn't working below: I can't make the genPrim function work, as I get the "TypeError: 'int' object is not subscriptable". 下面无法解决的问题:由于出现“ TypeError:'int'对象不可下标”,因此我无法使genPrim函数正常工作。

A few observations: 1. What my program should do is first input a number into a list and then apply the other functions on that number. 一些观察:1.我的程序应该做的是首先将一个数字输入到列表中,然后将其他功能应用于该数字。 2. The problem is that I can't seem to use that number from the list to do so. 2.问题是我似乎无法使用列表中的号码来执行此操作。 How can I do it? 我该怎么做? I was first thinking about asking for its position, but when going for the genPrim, both genPrim and Prim work because they are interdependent but they ask for the same thing. 我最初是想问问自己的位置,但是当选择genPrim时,genPrim和Prim都可以工作,因为它们是相互依存的,但它们都要求相同的东西。

def Adauga(L):
    n = int(input("Give number:"))
    L = L + [n]
    return L

#Verify if number is prime
def Prim(L):
    poz = int(input("Position of number: "))
    n = L[poz]
    if n<2 :
        return False
    NrDiv=0
    for a in range (2,int(n/2+1)):
        if n%a==0:
            NrDiv=NrDiv+1
    if (NrDiv==0):
        return True
    else:
        return False

#Generate prime number
def genPrim(L):
    poz = int(input("Give number: "))
    a = L[poz]
    b=a+1
    while Prim(b)==False:
        b=b+1
    return b

#Display menu
def AfisMeniu():
    print()
    print("1.Add number")
    print("2.Check if number is prime")
    print("3.Generate prime number")
    print("0.End of program")
    i = int(input("Your option: "))
    return i

def Main():
    """
      Start the program
    """
    L = []
    Opt = AfisMeniu()
    while (Opt != 0):
        if Opt == 1:
            L=Adauga(L)
        elif Opt ==2:
            L=Prim(L)
            print (L)
        elif Opt ==3:
            L=genPrim(L)
            print (L)
        else:
            print ("Wrong!")
        Opt = AfisMeniu()
    print()
    print("End of program")

Main()

You are getting that error because genPrim returns an int , but Main() assigns that result to L , so L no longer contains a list of numbers, just that single int . 因为genPrim返回一个int ,但Main()将该结果分配给L ,所以您得到了该错误,因此L不再包含数字列表,而仅包含单个int

Similarly, Prim() returns a boolean ( True or False ) but Main() assigns that to L , too. 同样, Prim()返回一个布尔值( TrueFalse ),但Main()将其分配给L

FWIW, the basic logic of your Prim() function is correct, but it's a very inefficient way to test if a number is prime. FWIW,您的Prim()函数的基本逻辑是正确的,但是它是测试数字是否为质数的非常无效的方法。 At the very least you should change it to return False as soon as it has found one divisor, ie when n%a==0 . 至少应将其更改为在找到一个除数后立即返回False ,即当n%a==0

I've managed to make the third option work, as in generating a prime number. 我设法使第三个选项起作用,就像生成素数一样。 However, what I would also like is to make the second option work as well, the prime verification. 但是,我也希望使第二个选项也可以工作,即主要验证。

My idea is modifying the code in the Main() function, by taking the len of the position, however I can't really make it work. 我的想法是通过获取该位置的len来修改Main()函数中的代码,但是我不能真正使其工作。

elif Opt ==2:
        L=Prim(L)
        poz1=int(len(L))
        print (L[poz1])

Or perhaps, should I attempt a different method? 还是应该尝试其他方法?

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

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