Question: ) You are to define Python function chaseSimple() within a standard python file ChaseSimple.py that simulates the chase from problem 1 but prints nothing and

) You are to define Python function chaseSimple() within a standard python file ChaseSimple.py that simulates the chase from problem 1 but prints nothing and instead returns True (if the imp escapes) or False (if it is recaptured). However, instead of using fixed positions and speeds, your function must accept the following parameters: 1. impSpd: A two-element tuple where element 1 is the min # feet per second and element 2 is the max # for the imp (instead of the static 1 and 7, respectively). 2. golemSpd: A two-element tuple identical to impSpd, but gives the golems movement in feet per second. 3. headStart: a single integer indicating the initial position of the imp in feet. 4. exitPosition: A single integer indicating the foot at which the exit appears.

The parameters must be passed to function chaseSimple in the above order. Its signature should therefore be chaseSimple(impSpd, golemSpd, headStart, exitPosition). The actual simulation is identical to that of problem 1, but should substitute the parameters in place of the fixed values.

This is the code to use.

import random # this function prints a line after each second def printline(G,I,t): # G = position of Golem # I = position of Imp # t = currenttime # loop for 50 feet for i in range(50): # if i is in integer range of G print G if(i==int(G)): print('G', sep='', end ='') # if i is in integer range of I print I elif(i==int(I)): print('I', sep='', end ='') # else prints - else: print('-', sep='', end ='') # prints the timestamp print('X ', t,'s', sep='') # initialize the position of Golem, Imp and the current time G = 0 I = 5 t = 0 # infinite loop, will break on condition while(True): # prints the line in current time printline(G,I,t) # advance golem between 3-4 feet G = G + 3 + random.random() # advance imp by 1-7 feet I = I + 1 + 6*random.random() # increase time t = t + 1 # check if imp escaped, then print message and break loop if(I>=50): print("THE IMP HAS ESCAPED TO FREEDOM AFTER {} SECONDS. MISCHIEF AND TRICKERY AWAIT!".format(t)) break # check if golem caught imp then also prints message and breaks if(G>=I): print("SADLY, ITS BACK TO THE TOWER FOR THE IMP AFTER A {} SECOND-CHASE.".format(t)) break

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!