Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use Python, from __future__ import print_function # Ladybug moves randomly looking for aphids to eat. # Each aphid gives her ten units of life energy.

Use Python,

image text in transcribedimage text in transcribed

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 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  self.location += dir # each move costs one unit of life energy. Add code here.   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!   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) ladybug.move() if ladybug.life_energy == 0: print(ANSI.BOLD + ANSI.RED + " Sorry, your ladybug has died. Not enough food." + ANSI.END) 
Open the starter code in the file ladybug.py A ladybug's world is confined to a single leaf which stretches from 0 to +100, and is discretized to these 101 locations. Ladybugs eat aphids which appear randomly on the leaf world Each aphid consumed gives the lady bug 10 units of energy. However, each move costs the ladybug one unit of life energy. Will she find enough food to stay alive? Can you help improve her odds'? a. Run the program. You should see output similar to this, showing the lady bug randomly foraging for food. The ANSI format tags were used to add color. Life_Energy: 100 Life Energy: 100 Life_Energy: 100 Life_Energy: 100 Life_Energy: 100 Life Energy: 100 Life Energy: 100 00O 0. 0 Oo 0 0 0 0o.o 00 O The green leaf stretches from 0 to 100. The ladybug is shown as a red X. The aphids (her only source of food) are shown as small o's in cyan You can see the code is incomplete. Find the move() method and add code so that each move costs the ladybug one unit of life_energy. If her life energy falls to zero, she is dead and the simulation stops b. Also in the moveO method add code so that if the ladybug finds an aphid, she gobbles it down and gains ten units of life energy. The code to remove the aphid has been given for free

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

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions