1

What isn't working below: I can't make the genPrim function work, as I get the "TypeError: 'int' object is not subscriptable".

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. 2. The problem is that I can't seem to use that number from the list to do so. 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.

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()
twasbrillig
  • 17,084
  • 9
  • 43
  • 67
Geosphere
  • 315
  • 4
  • 15
  • 1
    don't you get line number as part of that error message? what's the great idea not to include it here? – Will Ness Nov 15 '14 at 08:17

2 Answers2

1

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.

Similarly, Prim() returns a boolean (True or False) but Main() assigns that to L, too.

FWIW, the basic logic of your Prim() function is correct, but it's a very inefficient way to test if a number is prime. At the very least you should change it to return False as soon as it has found one divisor, i.e. when n%a==0.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • Thank you, I managed to solve. I just deleted the first two lines from Prim, and changed it parameter from L to n. Problem solved. – Geosphere Nov 15 '14 at 08:42
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.

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

Or perhaps, should I attempt a different method?

Geosphere
  • 315
  • 4
  • 15