Skip to content

Instantly share code, notes, and snippets.

@pdbartsch
Created February 5, 2019 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdbartsch/dd00635c32a2791c64693b2c76945164 to your computer and use it in GitHub Desktop.
Save pdbartsch/dd00635c32a2791c64693b2c76945164 to your computer and use it in GitHub Desktop.
# https://www.practicepython.org/exercise/2014/02/26/04-divisors.html
import math
invar = input("Please enter an integer. Then I'll return a list of all divisors of that number: ")
def divisors(n):
divisors = [ x for x in range(1,n+1) if n % x == 0]
print(*divisors, sep=', ')
try:
# deal with float inputs
n = math.floor(float(invar))
ceil = math.ceil(float(invar))
if ceil - n > 0:
print('Your number was rounded to ' + str(n) + '. The divisors of ' + str(n) + ' are:')
else:
print('The divisors of ' + str(n) + ' are:')
divisors(n)
# deal with non-numeric input
except:
print("I don't think that was a number. Try again.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment