Created
September 26, 2016 03:48
-
-
Save deadlysyn/b23fc1059fe911424f4311862abd52a0 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 3: | |
# given a list, print elements that are less than 5. | |
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 | |
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] | |
# main exercise | |
for x in a: | |
if x < 5: | |
print(x) | |
# extra 1 | |
b = [] | |
for x in a: | |
if x < 5: | |
b.append(x) | |
print(b) | |
# extra 2 | |
print([x for x in a if x < 5]) | |
# extra 3 | |
num = get_num('Enter a number: ') | |
print([x for x in a if x < num]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment