Skip to content

Instantly share code, notes, and snippets.

@deadlysyn
Last active September 26, 2016 05:11
Show Gist options
  • Save deadlysyn/672487f243375182c735e81ac8dfa6f6 to your computer and use it in GitHub Desktop.
Save deadlysyn/672487f243375182c735e81ac8dfa6f6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# practicepython.org exercise 4:
# given a number, print all divisors.
def get_num(prompt='Number? '):
"""helper function to prompt for a number"""
_num = 0
while True:
try:
_num = int(input(prompt))
except ValueError:
print('Was that a number? Try again!')
continue
else:
break
return _num
num = get_num('Enter a number: ')
# with a loop...
result = []
for x in range(1, num + 1):
if num % x == 0:
result.append(x)
print(result)
# with a listcomp...
a = list(range(1, num + 1))
b = [x for x in a if num % x == 0]
print(b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment