Created
September 1, 2020 03:52
-
-
Save Zeyu-Li/0651f488bf5b8d7229a903441b3ec0f1 to your computer and use it in GitHub Desktop.
generates a level of the pascal triangle
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
''' | |
Pascal's Triangle by Andrew Li | |
Prints out the level of the pascal's triangle requested by the user | |
''' | |
def next_lvl(old_level): | |
length = len(old_level) | |
new_lvl = [1] | |
for i in range(length - 1): | |
new_lvl.append(old_level[i] + old_level[i+1]) | |
return new_lvl + [1] | |
def get_level(lvl_request): | |
# special cases | |
# level 1 is just 1 | |
if lvl_request == 1: | |
print([1]) | |
return | |
elif lvl_request == 2: | |
print([1, 1]) | |
return | |
level = [1, 1] | |
for _ in range(lvl_request - 2): | |
level = next_lvl(level) | |
print(level) | |
def main(): | |
try: | |
lvl_request = int(input('What level in pascal\'s triangle do you want (level 1 is just 1): ')) | |
assert lvl_request >= 0, 'Level must be over 1' | |
except ValueError: | |
print('That is not a valid request') | |
return | |
except AssertionError as error: | |
print(error) | |
return | |
get_level(lvl_request) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment