Skip to content

Instantly share code, notes, and snippets.

@misterhtmlcss
Last active September 19, 2022 15:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misterhtmlcss/62abf55b63dd3ebd4e3e9c805b0661cf to your computer and use it in GitHub Desktop.
Save misterhtmlcss/62abf55b63dd3ebd4e3e9c805b0661cf to your computer and use it in GitHub Desktop.
Learning Journal Assignment for Unit 3 (Python) - 2020
from colorama import Fore
# 1. countdown & countup application
# ✓ The code of your program.
# ✓ Output for the following input: a positive number, a negative number, and zero.
# ✓ An explanation of your choice for what to call for input of zero.
# Count down from a positive number
def countdown(n):
if n == 0:
print(Fore.YELLOW + 'Blastoff!')
else:
print(n)
countdown(n - 1)
# Count up from a negative number
def countup(n):
if n == 0:
print(Fore.GREEN + 'Blastoff!')
else:
print(n)
countup(n + 1)
# I took some creative license here. I ask for your patience; I do start writing the assignment strictly as asked, but then I just keep going and I don't want to submit just the basics of what is asked.
def main():
# The below was inspired, but not derived from the reference below (Python Basics, n.d.).
try:
starting_count = int(input(Fore.RESET + '\nHow long would you like the countdown to be: '))
# In the end I decided to add this first condition because it will make for faster code. The others would require a function call and I believe 2 more layers to the stack versus my immediate exit with 0.
if starting_count == 0:
print('\n' + Fore.RED + 'No countdown for you....Blastoff!')
elif starting_count < 0:
return countup(starting_count)
else:
return countdown(starting_count)
# Error handling if the user inputs a non-binary input
except:
print('\n' + Fore.RED + 'You caused an error. Please enter numbers only\n')
main()
# 2. RuntimeError
def main2():
# Removed the int() method and now input is a string and not a integer, meaning the program will error at runtime.
starting_count = input(Fore.RESET + '\nHow long would you like the countdown to be: ')
if starting_count == 0:
print('\n' + Fore.RED + 'No countdown for you....Blastoff!')
elif starting_count < 0:
return countup(starting_count)
else:
return countdown(starting_count)
# Calls main and calls the countdown and countup functions.
# I used a try/except because human input is prone to faults and I wanted to control error management.
if __name__ == "__main__":
main()
main2()
# Output demonstrating the runtime error, including the error message.
# Error Message:
# How long would you like the countdown to be: d
# Traceback (most recent call last):
# File "u3.py", line 62, in <module>
# main2()
# File "u3.py", line 52, in main2
# elif starting_count < 0:
# TypeError: '<' not supported between instances of 'str' and 'int'
# An explanation of the error message.
# The above message is due to the input being a string and not a integer.
# An explanation of how to fix the error.
# add the int() method to convert the input to a number. This doesn't prevent non-binary entries from erroring which is why my original code used a Try/Except block.
# Reference
# Python Basics. (n.d.). Try and Except in Python. Retrieved from: https://pythonbasics.org/try-except/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment