Position of the largest prime number in a list

1

Good afternoon this is my code it took almost 2 hours looking at what I'm wrong and I could not find out please could you help me

'' 'Read 10 integers, store them in a list and determine in which position of the list is the highest number read' ''

try:

lista=[]
pos=0

for i in range(10):
    numero=int(input("Digite un numero entero: "))
    lista.append(numero)

mayor=lista[0]
aumento=0

for l in range(len(lista)): 
    if (lista[l]%l)==0:
        aumento+=1

    if aumento==2:
        if lista [l] > mayor:
            lista[l] = mayor
            pos=l+1

print("El numero primo mayor se encuentra en la posicion %d"%pos)
    
asked by Andress115 23.09.2018 в 19:56
source

3 answers

0

I think that is where you assign the value to "greater"

 mayor=lista[0]

You assign the oldest number stored in your list arrangement to greater, therefore the first stored number will always be the one you are comparing

    
answered by 23.09.2018 в 20:08
0

Well, first of all I do not know what you wanted to do here:

if (lista[l]%l)==0:
    aumento+=1

And here:

if aumento==2:
    if lista [l] > mayor:
        lista[l] = mayor
        pos=l+1

You have it backwards! I explain why: If the number in position x is greater than the number stored in the variable 'major', then you have to store that number from position x in 'major' and not match the item in the list to the value of 'major' because this would erase the number that is greater than the one in 'major' (what a mess haha)

In summary, it would be this:

if aumento==2:
    if lista [l] > mayor:
        # Puesto que lista[l] es mayor a 'mayor'
        # Tomo el elemento lista[l] como mi nuevo 'mayor'
        mayor = lista[l] 
        pos=l+1

If at the top, you wanted to check if a number is prime or not, I recommend you do something like that inside the for:

for k in range(1,(lista[l]+1)): # Compruebo desde 1 hasta el numero 
    if(lista[l]%k==0):          # Por cuanto de los numeros es divisible
        aumento+=1
    
answered by 23.09.2018 в 21:02
0

I think that for now you are skipping the problem of knowing whether the number is prime or not, and you are in the part of determining the maximum number on the list.

In that case it should be:

mayor=0
pos=0

for l in range(len(lista)): 
    if lista [l] > mayor:
       mayor = lista[l]
       pos=l+1

print("El numero primo mayor se encuentra en la posicion %d"%pos)

I put the principle that the major is zero and it is in the zero position, because this way I treat all the iterations equally regardless of whether they are the first or the other 9.

But it is still pending to solve the other, and it is not trivial. (Although it is very googleable)

    
answered by 24.09.2018 в 03:41