Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CPT 180 Chapter 3 Assignment 2 Directions Using the following guidelines, create a python program. 1. Create the zigzag.py program from the directions contained in

image text in transcribedimage text in transcribedimage text in transcribed

CPT 180 Chapter 3 Assignment 2 Directions Using the following guidelines, create a python program. 1. Create the zigzag.py program from the directions contained in the Zigzag.pdf file. (This is also listed on pages 72 - 74 of the 2nd Edition.) 2. Add three comment lines at the top of the program that contain: a. Program Name b. Program Description C. Programmer's Name [You) 3. Add code to prompt the user for the number of asterisks to print. Only allow integers between 5 and 25. If a non-integer is entered or an integer that is too small or too large display an appropriate error message and re-prompt the user. 4. Add code in the program that prints START! when the sequence starts (indent is 0) and STOP! when the sequence reverses (indent is at maximum) 5. See below for an Example Output. 6. Submit the zigzag.py file into the Chapter 3 Assignment 2 Submission Folder. Hints 1. The error or exception that occurs when the int() function cannot convert a string to an integer is the ValueError exception. Let's use the programming concepts you've learned so far to create a small animation program. This program will create a back and forth, zigzag pat tern until the user stops it by pressing the Mu editor's Stop button or by pressing CTRL-C. When you run this program, the output will look some- thing like this. ******** ******** ******** ******** ******** ******** ******** ******** ******** Type the following source code into the file editor, and save the file as zigzag py: import time, sys indent = 0 # How many spaces to indent. indent Increasing - True # Whether the indentation is increasing or not. try: while True: # The main program loop. print(' ' * indent, end="') print("********') time.sleep(0.1) # Pause for 1/10 of a second. if indent Increasing: # Increase the number of spaces: indent = indent + 1 if indent -- 20: # Change direction: indent increasing - False else: # Decrease the number of spaces: indent = indent - 1 if indent == 0: # Change direction: indent Increasing = True except KeyboardInterrupt: sys.exit() Let's look at this code line by line, starting at the top. import time, sys indent = 0 # How many spaces to indent. indent Increasing - True # Whether the indentation is increasing or not. First, we'll import the time and sys modules. Our program uses two vari- ables: the indent variable keeps track of how many spaces of indentation are before the band of eight asterisks and indent Increasing contains a Boolean value to determine if the amount of indentation is increasing or decreasing try: while True: # The main program loop. arsintisindent and Next, we place the rest of the program inside a try statement. When the user presses CTRL-C while a Python program is running, Python raises the KeyboardInterrupt exception. If there is no try-except statement to catch this exception, the program crashes with an ugly error message. However, for our program, we want it to cleanly handle the KeyboardInterrupt exception by calling sys.exit(). (The code for this is in the except statement at the end of the program.) The while True: infinite loop will repeat the instructions in our pro- gram forever. This involves using '' indent to print the correct amount of spaces of indentation. We don't want to automatically print a newline after these spaces, so we also pass end="' to the first print() call. A second print() call prints the band of asterisks. The time.sleep() function hasn't been cov ered yet, but suffice it to say that it introduces a one-tenth-second pause in our program at this point. if indent Increasing: # Increase the number of spaces: indent - indent + 1 if indent -- 20: indent Increasing = False # Change direction. Next, we want to adjust the amount of indentation for the next time we print asterisks. If indent Increasing is True, then we want to add one to indent. But once indent reaches 20, we want the indentation to decrease. else: # Decrease the number of spaces: indent = indent. 1 if indent - 0: indent Increasing - T = True # Change direction. Meanwhile, if indent Increasing was false, we want to subtract one from indent. Once indent reaches o, we want the indentation to increase once again. Either way, the program execution will jump back to the start of the main program loop to print the asterisks again. except KeyboardInterrupt: sys.exit() If the user presses CTRL-C at any point that the program execution is in the try block, the KeyboardInterrrupt exception is raised and handled by this except statement. The program execution moves inside the except block, which runs sys.exit() and quits the program. This way, even though the main program loop is an infinite loop, the user has a way to shut down the program. CPT 180 Chapter 3 Assignment 2 Directions Using the following guidelines, create a python program. 1. Create the zigzag.py program from the directions contained in the Zigzag.pdf file. (This is also listed on pages 72 - 74 of the 2nd Edition.) 2. Add three comment lines at the top of the program that contain: a. Program Name b. Program Description C. Programmer's Name [You) 3. Add code to prompt the user for the number of asterisks to print. Only allow integers between 5 and 25. If a non-integer is entered or an integer that is too small or too large display an appropriate error message and re-prompt the user. 4. Add code in the program that prints START! when the sequence starts (indent is 0) and STOP! when the sequence reverses (indent is at maximum) 5. See below for an Example Output. 6. Submit the zigzag.py file into the Chapter 3 Assignment 2 Submission Folder. Hints 1. The error or exception that occurs when the int() function cannot convert a string to an integer is the ValueError exception. Let's use the programming concepts you've learned so far to create a small animation program. This program will create a back and forth, zigzag pat tern until the user stops it by pressing the Mu editor's Stop button or by pressing CTRL-C. When you run this program, the output will look some- thing like this. ******** ******** ******** ******** ******** ******** ******** ******** ******** Type the following source code into the file editor, and save the file as zigzag py: import time, sys indent = 0 # How many spaces to indent. indent Increasing - True # Whether the indentation is increasing or not. try: while True: # The main program loop. print(' ' * indent, end="') print("********') time.sleep(0.1) # Pause for 1/10 of a second. if indent Increasing: # Increase the number of spaces: indent = indent + 1 if indent -- 20: # Change direction: indent increasing - False else: # Decrease the number of spaces: indent = indent - 1 if indent == 0: # Change direction: indent Increasing = True except KeyboardInterrupt: sys.exit() Let's look at this code line by line, starting at the top. import time, sys indent = 0 # How many spaces to indent. indent Increasing - True # Whether the indentation is increasing or not. First, we'll import the time and sys modules. Our program uses two vari- ables: the indent variable keeps track of how many spaces of indentation are before the band of eight asterisks and indent Increasing contains a Boolean value to determine if the amount of indentation is increasing or decreasing try: while True: # The main program loop. arsintisindent and Next, we place the rest of the program inside a try statement. When the user presses CTRL-C while a Python program is running, Python raises the KeyboardInterrupt exception. If there is no try-except statement to catch this exception, the program crashes with an ugly error message. However, for our program, we want it to cleanly handle the KeyboardInterrupt exception by calling sys.exit(). (The code for this is in the except statement at the end of the program.) The while True: infinite loop will repeat the instructions in our pro- gram forever. This involves using '' indent to print the correct amount of spaces of indentation. We don't want to automatically print a newline after these spaces, so we also pass end="' to the first print() call. A second print() call prints the band of asterisks. The time.sleep() function hasn't been cov ered yet, but suffice it to say that it introduces a one-tenth-second pause in our program at this point. if indent Increasing: # Increase the number of spaces: indent - indent + 1 if indent -- 20: indent Increasing = False # Change direction. Next, we want to adjust the amount of indentation for the next time we print asterisks. If indent Increasing is True, then we want to add one to indent. But once indent reaches 20, we want the indentation to decrease. else: # Decrease the number of spaces: indent = indent. 1 if indent - 0: indent Increasing - T = True # Change direction. Meanwhile, if indent Increasing was false, we want to subtract one from indent. Once indent reaches o, we want the indentation to increase once again. Either way, the program execution will jump back to the start of the main program loop to print the asterisks again. except KeyboardInterrupt: sys.exit() If the user presses CTRL-C at any point that the program execution is in the try block, the KeyboardInterrrupt exception is raised and handled by this except statement. The program execution moves inside the except block, which runs sys.exit() and quits the program. This way, even though the main program loop is an infinite loop, the user has a way to shut down the program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions