Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Ask the user for a number and determine whether the number is prime or not.
- # (For those who have forgotten, a prime number is a number that has no divisors.).
- # You can (and should!) use your answer to Exercise 4 to help you.
- # Take this opportunity to practice using functions, described below.
- def get_user_input():
- while True:
- user_input = raw_input("Enter your number: ")
- if user_input.isdigit() and int(user_input) != 0:
- return int(user_input)
- else:
- print ("Wrong input. ")
- def is_prime(user_num):
- divisors = [i for i in range(1, user_num+1) if user_num % i == 0]
- if len(divisors) == 1 or len(divisors) == 2:
- return True
- else:
- return False
- def main():
- while True:
- number = get_user_input()
- if is_prime(number):
- print ("The number you have given is a prime.")
- return False
- else:
- print ("The number you have given is not a prime.")
- return False
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement