+ 1

How to test if two separate user input is a prime number?

I have a function isPrime() that prompts the user to enter two input p then q and test if they are prime. If the numbers are prime then it should call the genKeys() function; if not it should print ‘the number is not prime’ and prompt the user to re-enter a number. However, it’s not working properly. If the numbers are prime, it doesn’t print the info from the genKey() function. Can someone assist me please? I’d really appreciate the help. https://code.sololearn.com/cosovskxBTan/?ref=app

25th Nov 2022, 10:45 PM
Triz
Triz - avatar
2 Antworten
+ 1
The code you attached, looks incomplete and taken out of context. If this is the signature of the genKeys function: def genKeys(self, p, q): it implies that this is a method of a class, and you need an instance of this class to run this function. In your isPrime function you try to call it like this: self.genKeys(self.p, self.q) But isPrime is also not part of any class, and in your code it has no awareness at all of the self variable (which represents the object instance). The signature of this method should be def isPrime(self, p, q): And it should be inside the same class as genKeys, for this to work. You seem to be missing the basic concepts of OOP programming. You either have to provide the complete code, or write a simplified function that only focuses on the algorithm of figuring out if two numbers are both primes (or co-primes of each other, meaning they have no common denominator, it's not clear which case you want to check.
26th Nov 2022, 9:11 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Another note, these two lines: while True: try: Cannot be indented at the same level, this is syntax error. I suggest you post a minimum working code that is testable, then we can continue debugging what is wrong with your algorithm.
26th Nov 2022, 9:13 AM
Tibor Santa
Tibor Santa - avatar