-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9_is_prime.py
53 lines (40 loc) · 1.42 KB
/
day9_is_prime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# ECX 30 DAYS OF CODE AND DESIGN
# Day 9
"""
**Is Prime?**
Task: \n
Write a function that takes an integer as input, and determines whether it is a prime number or not.
"""
def prime_number(int_input):
"""Checks if a user's input is either a prime or composite number."""
prime = True
# Negative numbers are not prime numbers
if int_input < 0:
print('Negative numbers are not prime numbers.')
prime = False
# 0 and 1 are not prime numbers
if int_input == 0 or int_input == 1:
print(str(int_input) + ' is a composite (not a prime) number.')
prime = False
# Divide the number inputted by the user from two to the int_input - 1
for i in range(int_input):
# Prevent ZeroDivisionError and division by 1
if i == 0 or i == 1:
continue
# Check if the number is a composite (not a prime) number
if int_input % i == 0:
print(str(int_input) + ' is a composite (not a prime) number.')
prime = False
break
# Print if the number is prime
if prime is True:
print(str(int_input) + ' is a prime number.')
# User's input
try:
print(' Prime Number Checker '.center(30, '*'))
print('Check if a number is a prime or composite number.')
user_input = int(input('Enter number: '))
# Function call
prime_number(user_input)
except ValueError:
print('Invalid input! Input only integers')