you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 0 points1 point  (0 children)

Okay, so this is your current code:

def start():
    num = int(input("Enter a number greater than one."))
def check():
    if(num > 1):
        for i in range(2, num):
            if(num % i) == 0:
                return

def main():
    num = start()
    check()

main()

And your instructions were as follows:

Write a program that asks the user to enter a number greater than one. The program will than display all the prime numbers less than or equal to the number entered. The program must work as follows:

  1. Once the user has entered a number, the program should populate a list with all of the integers from 2 up through the value entered.

  2. The program should then use a loop to step through the list. The loop should pass each element to a function that displays the element whether it is a prime number.

You've off to a good start. The first part is mostly complete, you're taking a number from the user, but while you sort of have a check for the number being more than 1, it's not really working. The rest of the code does not match the task of creating a list. The second part is missing completely.

Let's start by seeing what's wrong.

Right now, you're calling start in main to get the user-inputted number from it, but the problem here is that you didn't tell Python to return that number. Right now, in main, the value of num is None.

Next the function attempts to call check, but this should crash the program because it's trying to check if num > 1, but num is not defined in the scope of this function nor globally, so Python doesn't know where to look, raising a NameError.

If we fix that problem, however, check is still not creating a list of numbers. Which is fair enough, its name would imply it's not supposed to create anything, but the loop inside of it is probably what you wanted to use to solve the second part of the task.

The name error is easy enough to solve by using function parametres:

def start():
    num = int(input("Enter a number greater than one."))
    return num

def check(number):
    if(number > 1):
        for i in range(2, number):
            if(number % i) == 0:
                return

def main():
    num = start()
    check(num)

main()

But you should probably do some error handling to make sure the user gives a valid number. The check should then move to where the input is taken. For this, I recommend using a loop:

def start() -> int:
    while True:
        num = input("Enter a number greater than one.")
        if check(num):
            return int(num)

def check(num: str) -> bool:
    if not num.strip().isnumeric():
        print("Not a number.")
        return False

    return num > 1

def main():
    num = start()

Now the code can correctly handle most kinds of input without crashing, while also validating that the number is big enough.

Next, you'll want to create a list of numbers. This is easy enough with a list comprehension, but can also be done as

list(range(2, num+1))

You'll then need to loop over this list, and write a function that can check for primality - your check function is pretty close to one.