I wrote an answer to a program that gets a number from the user and display whether the number is prime.This is a beginner exercise.
Here is the question:
A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3 and 6. Write a Boolean function named is_prime which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Use the function in a program that prompts the user to enter a number then displays a message indicating whether the number is prime.
Program I wrote :
def main():
# Get a number
number = get_number()
# Check the number is a prime
status = is_prime(number)
# Display a message if the number is prime or not
display_prime(status)
def get_number():
number = int(input('Enter a number: '))
return number
def is_prime(number):
if (number % 1 == 0) and (number % number == 0):
status = True
else:
status = False
return status
def display_prime(status):
if is_prime(status):
print('The number is a prime')
else:
print('The number is not a prime.')
main()
Here is the output:
Enter a number: 6
The number is a prime
I can't find where I went wrong. Can you help me ?
Thank you in advance.
CodePudding user response:
Where you went wrong is you ignored the word "only" in the definition of a prime number:
A prime number is a number that is only evenly divisible by itself and 1
Every integer, prime or otherwise, is evenly divisible by itself and 1. The difference is that a prime number is only divisible by itself and 1. Because your function is simply checking that the input is divisible by itself and by 1, every input results in returning true.
What you want to do instead is write a function that determines that the number is not evenly divisible by any other number other than itself and 1.
There are some good examples of how to do that here: https://www.programiz.com/python-programming/examples/prime-number
CodePudding user response:
There is no need to call is_prime
again in display_prime
:
def get_int_input(prompt: str = 'Enter an integer number: ') -> int:
number = -1
while True:
try:
number = int(input(prompt))
break
except ValueError:
print('Error: Enter an integer, try again...')
return number
def is_prime(number: int) -> bool:
"""Checks if a number is a prime by division"""
if number < 2:
return False
if number == 2:
return True
for x in range(2, int(number**0.5) 1):
if number % x == 0:
return False
return True
def display_prime(prime_status: bool) -> None:
"""Displays a message if the number is prime or not"""
if prime_status:
print('The number is a prime.')
else:
print('The number is not a prime.')
def main() -> None:
number = get_int_input()
status = is_prime(number)
display_prime(status)
if __name__ == '__main__':
main()
Example Usage 1:
Enter an integer number: 6
The number is not a prime.
Example Usage 2:
Enter an integer number: a
Error: Enter an integer, try again...
Enter an integer number: 2860486313
The number is a prime.