From colorama import Fore PDF

Title From colorama import Fore
Course Learning Journal Unit 1 CS1101
Institution University of the People
Pages 4
File Size 42.4 KB
File Type PDF
Total Downloads 21
Total Views 137

Summary

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....


Description

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 numberr 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 #

main2()

# File "u3.py", line 52, in main2 #

elif starting_count < 0:

# TypeError: '...


Similar Free PDFs