Question: Use Python to fix the code from __future__ import print_function # Ladybug moves randomly looking for aphids to eat. # Each aphid gives her ten
Use Python to fix the code


from __future__ import print_function # Ladybug moves randomly looking for aphids to eat. # Each aphid gives her ten units of life energy. # Each move costs her one unit of life energy import random import time import numpy as np class ANSI: MAGENTA = '\033[95m'; CYAN = '\033[96m'; DARKCYAN = '\033[36m' BLUE = '\033[94m'; GREEN = '\033[92m' ; YELLOW = '\033[93m' RED = '\033[91m'; GREY = '\033[90m'; BLACK = '\033[30m' BOLD = '\033[1m'; UNDERLINE = '\033[4m'; END = '\033[0m' leaf_world = range(0, 101) # The world is a 1D leaf stretching from x=0 to x=100 aphid_locations = random.sample(leaf_world, 20) # the lady bug needs to eat aphids to stay alive! class Ladybug(object): def __init__(self, location=50, life_energy=100): self.name = "Ladybug" self.location = location self.life_energy = life_energy def __str__(self): return "{}: Location={} Life_Energy={}".format(self.name, self.location, self.life_energy,) def move(self): # dir = random.choice([-1, +1]) # move in a random direction uncomment if u want random walk # Moving intelligently towards the closest aphid if(len(aphid_locations)==0): print ("All Aphids eaten!!") return True dir = random.choice([-1, +1]) # Move towards the closest aphid diff_list = list(np.abs(np.array(aphid_locations)-self.location)) location_to_move_towards = (aphid_locations[(diff_list.index(np.min(diff_list)))]) if(location_to_move_towards # each move costs one unit of life energy. Add code here. self.life_energy -=1 if self.location in aphid_locations: aphid_locations.remove(self.location) # add code to increase her life_energy by ten! She just ate an aphid! self.life_energy += 10 def show_world(self): for x in leaf_world: if x==self.location: print(ANSI.RED + ANSI.BOLD + "X" + ANSI.END, end="") elif x in aphid_locations: print(ANSI.CYAN + ANSI.BOLD + "o" +ANSI.END, end="") else: print(ANSI.GREEN + ANSI.BOLD + "_" +ANSI.END,end="") print(" Life_Energy: ", self.life_energy) # show life_energy and start a new line if __name__ == "__main__": # Construct a new ladybug # She lands on the leaf at location 50, with 100 units of life energy ladybug = Ladybug(location=50, life_energy=100) while ladybug.life_energy: ladybug.show_world() time.sleep(0.25) if(ladybug.move()): break if ladybug.life_energy == 0: print(ANSI.BOLD + ANSI.RED + " Sorry, your ladybug has died. Not enough food." + ANSI.END) Exercise 5: Hungry Ladybug Survival! Clone your ladybug code in a new file, because you are about to make major changes. a. Add one aphid at a random location on the leaf on 10% of the moves. They reproduce like mad as any gardener knows. Use the random nodule. This will give the ladybug a better chance to stay alive Tip: At the top level, outside of the Ladybug class, create a method to add an aphid to the worldleaf. Here's some starter code. The starter code adds the aphid at 0. Fix that to be a random spot on the leaf. def add_aphid): global leaf_world global aphid_locations # adds one aphid to the list named leaf-world when called # we have to be careful not to add the aphid at the same spot where there's already an aphid # use random .randint() to pick a random integer from 0 to 100 n-0 # fix this stub. # Use a while loop to make sure n does not already have an aphid. # if it does, pick a new n aphid locations [n] In the while-loop inside the main method, you can now add an aphid about 10% of the time using this if statement. if not random.randint (0,9) add-aphid() # 10% probability to add one aphid at a random ocation on the leaf b. If the ladybug arrives at either end of the leaf, (location 0 or 100). Add code so she flies to a new leaf arriving at a random location. You will need to construct the new leaf_world and populate it with aphids. Exercise 5: Hungry Ladybug Survival! Clone your ladybug code in a new file, because you are about to make major changes. a. Add one aphid at a random location on the leaf on 10% of the moves. They reproduce like mad as any gardener knows. Use the random nodule. This will give the ladybug a better chance to stay alive Tip: At the top level, outside of the Ladybug class, create a method to add an aphid to the worldleaf. Here's some starter code. The starter code adds the aphid at 0. Fix that to be a random spot on the leaf. def add_aphid): global leaf_world global aphid_locations # adds one aphid to the list named leaf-world when called # we have to be careful not to add the aphid at the same spot where there's already an aphid # use random .randint() to pick a random integer from 0 to 100 n-0 # fix this stub. # Use a while loop to make sure n does not already have an aphid. # if it does, pick a new n aphid locations [n] In the while-loop inside the main method, you can now add an aphid about 10% of the time using this if statement. if not random.randint (0,9) add-aphid() # 10% probability to add one aphid at a random ocation on the leaf b. If the ladybug arrives at either end of the leaf, (location 0 or 100). Add code so she flies to a new leaf arriving at a random location. You will need to construct the new leaf_world and populate it with aphids
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
