Question about functions

I’m trying to write a function that determines if a number entered is prime or not. However, I want it to continue for 5 sessions before stopping. I thought I was doing that with this code but apperantly not. What am I missing? I´m defining the function, giving it tasks and then calling it at the end. That’s all right?
Thanks in advance

def prime_tester(num = int(input("Enter a number: "))):
    count = 0
    if num:
        count+=1
    while count > 5:
        if num % 2 != 0:
            print("It's prime!")
        else:
             print("Not prime")
prime_tester()

There are a number of ways to solve a problem like this. One thing you can do, that keeps everything contained within the function, is to track the number of sessions you’ve executed by passing those into prime_tester function itself. And take the num = int(input("Enter a number: ")) out of the parameter and instead put it as its own line in the function. Remove any line with the count variable completely, as it is unnecessary, and instead just output whether num is prime or not, add to the current number of sessions executed, and then execute the function from within itself with the new number of sessions executed. This is recursion at work.

If you get too frustrated, I can show you the code I have with comments on it, too, so you can see what’s happening.

Did you actually meant to test odd number, rather than prime number?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.