Question: Hello. I've been stuck on this assignment for awhile and can'tseem to figure out how to properly execute it. I've already donethe parts I can,
Hello. I've been stuck on this assignment for awhile and can'tseem to figure out how to properly execute it. I've already donethe parts I can, the part I'm struggling with is adding exceptionhandling to the lines indicated.
import randomimport timedef countdown(n): while n >= 0: print n time.sleep(1) n -= 1# Self Destruct Custom Function# This is the custom function created to handle all of the Self Destruct# features needed. There are a few steps involved in the process, so take a few# moments to study how this function works and think about ways to make it better.def self_destruct(x): # Set Destruct Codes authorized_test = "000-Destruct-0" authorized_final = "000-Destruct-1" # Display Self Destruct Warning print "--------------------- WARNING! ----------------------" print "You have initiated the USR ARES Self Destruct Program" print "_____________________________________________________" print "You must provide Authorized Initiate Code to Proceed." # This next section of code is designed to prevent the user from entering the wrong information while True: try: # Request Authorized Rank rank = int(raw_input( "Select Rank: [1] Commanding Officer [2] Executive Officer [3] Chief Engineer RANK: ")) # Because we're expecting the user to enter a number above, the conditional statement # below is needed to convert those numbers into something more useful. Doing this helps # reduce the risk of the user introducing bad data into the script. # Retrieve Rank Initiate Code if rank == 1: code = "111A-Destruct" print "Commanding Officer Confirmed." break # Executive Officer elif rank == 2: code = "21A2B-Destruct" print "Executive Officer Confirmed." break # Chief Engineer elif rank == 3: code = "31B2B-Destruct" print "Chief Engineer Confirmed." break else: print "You are not authorized to initial Self Destruct." except ValueError: print "You have entered an invalid code. Please try again." ####################################################################### # ADD EXCEPTION HANDLING TO THE FOLLOWING SECTION OF CODE # # The user must enter a correct code in order to proceed with the # # self destruct procedure. # # # # The script should force the user to enter a valid destruct code or # # or offer the option to abort the Self Destruct request entirely. # # Use the Exception Handling in the Section above as a reference. # ####################################################################### initiate = raw_input("Enter Self Destruct Confirmation Code: ") # Compare Rank Codes if initiate == code: print "Self Destruct Initiate Code: ACCEPTED" final_code = raw_input("Enter Activation Code: ") if final_code == authorized_final: print "Destruct Sequence Confirmed." # 10. EXPLAIN THE SIGNIFICANCE OF THE str() FUNCTION HERE print str(x) + " seconds to Self Destruct." print "ALL HANDS ABANDON SHIP - THIS IS NOT A DRILL" countdown(x) print "Have a nice day!" print "BOOM!" elif final_code == authorized_test: print "Destruct Sequence Test Order Confirmed." print "THIS IS A DRILL - THIS IS A DRILL" print "Timer Set to: " + str(x) + " seconds." else: print "Destruct Sequence Aborted." ######################################################################## Self Destructtimer = int(raw_input("Enter Countdown Length (in seconds): "))self_destruct(timer)
Step by Step Solution
3.51 Rating (151 Votes )
There are 3 Steps involved in it
To add exception handling to the code you can use tryexcept blocks to catch specific exceptions and handle them accordingly Heres an updated version o... View full answer
Get step-by-step solutions from verified subject matter experts
