Namneet(Python assignment 1 )

pdf

School

Seneca College *

*We aren’t endorsed by this school

Course

237

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

3

Uploaded by GeneralDragon2895

Report
Python 1 Python 1) class Person: def __init__(self, name, location): self.name = name self.location = location def introduce(self): print(f"Hello, my name is {self.name} and I am located at position {self.location} in the park environment.") class FoodItem: def __init__(self, name, location): self.name = name self.location = location class Agent: def __init__(self): self.location = 0 # pointer set to 0 so the agent starts at the beginning def move(self, steps): self.location += steps def encounter_person(self, person): if self.location == person.location: print(f"The agent encounters {person.name} in the park and barks.") def find_food(self, food_item): if self.location == food_item.location: print(f"The agent found {food_item.name} at location {food_item.location} in the park and eats it.") # Creating two Person objects, an Agent object, and a FoodItem first_name = Person("YourFirstName", 3) last_name = Person("YourLastName", 12) agent = Agent() food_at_location_9 = FoodItem("Pizza", 9) # Added a food item at the 9th location # Agent moves to the specified location agent.move(3) agent.encounter_person(first_name) agent.move(6) # Agent is now at location 9 agent.find_food(food_at_location_9) # Agent arrives at location 9 and eats the food 2) Add a new thing to the environment name it “Person” class Person: def __init__(self, name, age): self.name = name self.age = age def introduce(self):
Python 2 print(f"Hello, my name is {self.name} and I am {self.age} years old.") # Person object created person1 = Person("Jacob", 22) # Accessing attributes and methods of the Person object print(person1.name) # the value "Jacob" gets stored in the name variable and can be accessed via self.name print(person1.age) # the value "22" gets stored in the age variable and can be accessed via self.age person1.introduce() # Output: Hello, my name is Jacob and I am 22 years old. 3) Create two instances (objects) of the “Person” class and name the first instance your first name and set the location of this instance to be 3 in the park environment. Name the second instance your last name and set the location of this instance to be 12 in the park environment. class Person: def __init__(self, name, location): self.name = name self.location = location def introduce(self): print(f"Hello, my name is {self.name} and I am located at position {self.location} in the park environment.") # Person objects creation first_name = Person("YourFirstName", 3) last_name = Person("YourLastName", 12) # Accessing attributes and calling the introduce method first_name.introduce() last_name.introduce() 4) class Person: def __init__(self, name, location): self.name = name self.location = location def introduce(self): print(f"Hello, my name is {self.name} and I am located at position {self.location} in the park environment.") class Agent: def __init__(self): self.location = 0 # Pointer set to 0 so the agent starts at the beginning def move(self, steps):
Python 3 self.location += steps def encounter_person(self, person): if self.location == person.location: print(f"The agent encounters {person.name} in the park and barks.") # Creation of two Person class objects and one Agent object first_name = Person("YourFirstName", 3) last_name = Person("YourLastName", 12) agent = Agent() # Agent moves to the specified location via the use of ".move" function of the agent class agent.move(3) agent.encounter_person(first_name) agent.move(9) agent.encounter_person(last_name)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help