Write a program that prompts user to enter the start and end number and displays a list of prime numbers between start and end

Question:

I’m trying to import the python is_prime file into the python program, prime_between and to use the is_prime() function to display a list of prime numbers between two numbers. The program should return a list of prime numbers between the "start" and "end" numbers inclusive. Use the "list accumulator pattern" to build the list containing prime numbers.

def is_prime(num):
    if num <= 1:
        return False

    for n in range(2, num):
        if num % n == 0:
            return False

    return True
if __name__ == "__main__":
    num = int(input("Enter a number: "))
        if is_prime(num):
            print(f"{num} is a prime number")
        else:
            print(f"{num} is not a prime number")

""""""""""""""""""""""""""""""""""""""""""""""""""""

import prime

def prime_between(start, end):
    # Complete the prime_between function.
    prime_list = []
    for i in (start, end):
        x = prime(i)
        if x == "prime":
            prime_list.append(i)
        return prime_list
   
# Complete the main program.
if __name__ == "__main__":
    start = int(input("Enter the start number: "))
    end = int(input("Enter the end number: "))
    lst = prime_between(start, end)
    if len(lst) == 0:
        print("There are no prime numbers from", start, "to", end)
    else:
        print("The prime numbers from", start, "to", end, "are: ", lst)

When I ran it, I got

File "/home/prime_between.py", line 26, in <module>
    lst = prime_between(start, end)
File "/home/prime_between.py", line 17, in prime_between
    x = prime(i) 
TypeError: 'module' object is not callable
Asked By: 1TimeCoder

||

Answers:

When you import prime, you are importing all of the functions inside of that module (the module being the file prime.py). Check out this guide for a better overview on imports.

To use the code from your module, instead of x = prime(i), use x = prime.is_prime(i).

Another issue, in your following if statement, you check if it equals "prime", but your is_prime function returns True or False. A quick fix would be just if x: since x will be a boolean value.

Answered By: Chris

Import the function:

from prime import is_prime
...
x = is_prime(i)
...
Answered By: PublishPublication
import prime

def prime_between(start, end):
    # Complete the prime_between function.
    prime_list = []
    for i in range(start, end):
        prime_list = []
    for i in (start, end):
        x = prime.is_prime(i)
        if x is True:
            prime_list.append(i)
        return prime_list

You need to call the function from prime module and assign it to variable then check the return value.

Answered By: David Omen
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.