Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. # Ask the user for a number and determine whether the number is prime or not.
  2. # (For those who have forgotten, a prime number is a number that has no divisors.).
  3. # You can (and should!) use your answer to Exercise 4 to help you.
  4. # Take this opportunity to practice using functions, described below.
  5.  
  6.  
  7. def get_user_input():
  8. while True:
  9. user_input = raw_input("Enter your number: ")
  10. if user_input.isdigit() and int(user_input) != 0:
  11. return int(user_input)
  12. else:
  13. print ("Wrong input. ")
  14.  
  15.  
  16. def is_prime(user_num):
  17. divisors = [i for i in range(1, user_num+1) if user_num % i == 0]
  18. if len(divisors) == 1 or len(divisors) == 2:
  19. return True
  20. else:
  21. return False
  22.  
  23.  
  24. def main():
  25. while True:
  26. number = get_user_input()
  27.  
  28. if is_prime(number):
  29. print ("The number you have given is a prime.")
  30. return False
  31. else:
  32. print ("The number you have given is not a prime.")
  33. return False
  34.  
  35. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement