Last active
September 20, 2020 14:16
-
-
Save ahmad-ali14/148cf9027843ec16b7d4c69541760f21 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# count from a postive num descinding dwon to 0. | |
def countdown(n): | |
# base case | |
if n <= 0: | |
print('Blastoff!') | |
else: | |
print(n) | |
countdown(n-1) | |
# count from a negative num ascending up to 0. | |
def countup(n): | |
# base case | |
if -n <= 0: | |
print('Blastoff!') | |
else: | |
print(n) | |
countup(n+1) | |
# handling user input | |
def handle_user_input(str_input): | |
try: | |
# trnsforming the input string into number | |
num = int(str_input) | |
""" | |
if the user did not enter a valid number we argoing to face a RuntimeError | |
so everything is fine but the int() function can not work if we did not enter a valid number | |
we can handle this error by handling the Exception and show and appropriate error message that prevent our program from crashing | |
""" | |
except: | |
# handling if the user did not enter a valid number without breaking the program | |
return print("Error: please Enter a valid number \n") | |
# if num is positive | |
if num > 0: | |
countdown(num) | |
# if num is negative | |
elif num < 0: | |
countup(num) | |
# if num is 0 | |
else: | |
print(num, "\n") | |
if __name__ == "__main__": | |
# continue the program until you press ctrl + c to close the program | |
while True: | |
user_input = input("enter a number : \n") | |
handle_user_input(user_input) | |
print("___________________________________________________________\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment