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.