'Verify if there are any prime numbers on a list given by the user [duplicate]

I need a code on Python that receives as the input a list of numbers, and from that the program returns True if there are any prime numbers on the list or False if otherwise.

As a beginner I am only allowed to use basic functions (e.g if..else, while, range, for). I have tried writing this code so far, but it doesn't seem to be working:

## Function to check if a number is prime

def isPrime (x):
  if (x%2 !=0 and x%3 !=0 and x%5 !=0 and x%7 !=0):
    return True
  elif (x==2 or x==3 or x==5 or x==7):
    return True
  else:
    return False
## Verify if the list contains prime numbers
def primelist(*x):
  for i in x:
    if isPrime(i)==True:
      return True
    if isPrime(i)==False:
      return False
primelist = (3,6,8,9,12)

The expected outcome would be True once 3 is a prime, but I am getting no response. What am I doing wrong?



Solution 1:[1]

As other people mentioned, you can't have both a function and a variable named the same. The primelist function doesn't work. It returns False if the first element of the input is not a prime number. This will work.

def primelist(*x):
  for i in x:
    if isPrime(i)==True:
      return True
  
  return False

You should call this function like this:

a = primelist(3,6,8,9,12)

The variable a will contain the result (you can of course rename the variable as you please).

You should also check your isPrime function. Maybe this link will be helpful :)

Solution 2:[2]

First thing that you are doing wrong is naming both your function and the list as same. Second inside your function you are hard coding to check if the number is divisible by 2,3,5,7 etc. This is not correct. there are many numbers that are not divisible by 2,3,5 and 7 but are still not prime. You can try a function like this:

def isprime(list_name):
    for i in list_name:
        for n in range(2,int(i**0.5)+1):
            if i%n==0:
                return False
        return True

To call your function type this:

isprime(list_here)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pietro
Solution 2 Pushpesh Kumar