Created
April 3, 2018 16:27
-
-
Save Hwatwasthat/e51b95f783641a9a8e5bfbd21bbdaa72 to your computer and use it in GitHub Desktop.
Divisors within a prime checking program created by Hwatwasthat - https://repl.it/@Hwatwasthat/Divisors-within-a-prime-checking-program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def divisors(Num): | |
#Ensure number is positive | |
if Num <= 0: | |
print("Number must be positive") | |
main() | |
#create range to work in | |
range_nums = range(1, Num) | |
#create list for all divisors | |
list_divisors = [] | |
#iterate over numbers in range | |
for i in range_nums: | |
#check if number is divisor by checking if number modulo it is 0 | |
if Num % i == 0: | |
#add number to list if divisor | |
list_divisors.append(i) | |
return list_divisors | |
#function for taking the input, feeding to divisor function and outputting result | |
def primeness(): | |
# Take input to check | |
Num = int(input("\nWhat number do you want to check for primeness?")) | |
# feeding to divisor function | |
list_divisors = divisors(Num) | |
#print results, turning list elements into strings for printing | |
if (len(list_divisors) > 1): | |
print ("is not prime") | |
print ("List of divisors is:", (str(list_divisors)).strip('[]')) | |
elif Num == 1: | |
print ("1 is not a prime") | |
elif len(list_divisors) == 1: | |
print ("is prime") | |
else: | |
print("Error beep boop") | |
# Main loop of the program | |
def main(): | |
# First run goes into prime checking, we're assuming they started it for a reason | |
primeness() | |
# Infinite loop until exit is called | |
while True: | |
cont = input("\nWould you like to check another number?") | |
if cont == "yes": | |
primeness() | |
else: | |
exit() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment