Last active
September 26, 2016 05:11
-
-
Save deadlysyn/672487f243375182c735e81ac8dfa6f6 to your computer and use it in GitHub Desktop.
This file contains 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
#!/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