Python, Object-Oriented
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I recently took an exam for a class covering python. We were told to create a class called Place and two child classes, City and Home. The Place objects had a name and a location (None if not entered). City objects had the same but added a population and a mayor. Home objects had a name and location and added a number of beds and occupancy. Each object also had a visited boolean, starting at false.
Ex -->
indiana = Place('Indiana')
iu = Place('IU Campus', btown)
library = Place('Wells Library', iu)
btown = City('Bloomington', indiana, 400, 'Jim')
rental = Home('Rental House', btown, 4, 3)
I was supposed to implement a method visit() that changed the place's visited boolean to true and, if it was in a location change that location's visited boolean to true and print out as following...
Test code:
library.visit()
indiana.visit()
The output:
You visit Wells Library.
That means... You visit IU Campus.
That means... You visit Bloomington.
That means... You visit Indiana.
You already visited Indiana.
Any help with implementing the visit() method?
python python-3.x
add a comment |
I recently took an exam for a class covering python. We were told to create a class called Place and two child classes, City and Home. The Place objects had a name and a location (None if not entered). City objects had the same but added a population and a mayor. Home objects had a name and location and added a number of beds and occupancy. Each object also had a visited boolean, starting at false.
Ex -->
indiana = Place('Indiana')
iu = Place('IU Campus', btown)
library = Place('Wells Library', iu)
btown = City('Bloomington', indiana, 400, 'Jim')
rental = Home('Rental House', btown, 4, 3)
I was supposed to implement a method visit() that changed the place's visited boolean to true and, if it was in a location change that location's visited boolean to true and print out as following...
Test code:
library.visit()
indiana.visit()
The output:
You visit Wells Library.
That means... You visit IU Campus.
That means... You visit Bloomington.
That means... You visit Indiana.
You already visited Indiana.
Any help with implementing the visit() method?
python python-3.x
create a global list(initially empty one). check if you have already visited the place, if not append the place into the list.
– AkshayNevrekar
Nov 17 '18 at 6:28
2
What have you tried? Why didn't it work?
– Andrew Guy
Nov 17 '18 at 7:44
add a comment |
I recently took an exam for a class covering python. We were told to create a class called Place and two child classes, City and Home. The Place objects had a name and a location (None if not entered). City objects had the same but added a population and a mayor. Home objects had a name and location and added a number of beds and occupancy. Each object also had a visited boolean, starting at false.
Ex -->
indiana = Place('Indiana')
iu = Place('IU Campus', btown)
library = Place('Wells Library', iu)
btown = City('Bloomington', indiana, 400, 'Jim')
rental = Home('Rental House', btown, 4, 3)
I was supposed to implement a method visit() that changed the place's visited boolean to true and, if it was in a location change that location's visited boolean to true and print out as following...
Test code:
library.visit()
indiana.visit()
The output:
You visit Wells Library.
That means... You visit IU Campus.
That means... You visit Bloomington.
That means... You visit Indiana.
You already visited Indiana.
Any help with implementing the visit() method?
python python-3.x
I recently took an exam for a class covering python. We were told to create a class called Place and two child classes, City and Home. The Place objects had a name and a location (None if not entered). City objects had the same but added a population and a mayor. Home objects had a name and location and added a number of beds and occupancy. Each object also had a visited boolean, starting at false.
Ex -->
indiana = Place('Indiana')
iu = Place('IU Campus', btown)
library = Place('Wells Library', iu)
btown = City('Bloomington', indiana, 400, 'Jim')
rental = Home('Rental House', btown, 4, 3)
I was supposed to implement a method visit() that changed the place's visited boolean to true and, if it was in a location change that location's visited boolean to true and print out as following...
Test code:
library.visit()
indiana.visit()
The output:
You visit Wells Library.
That means... You visit IU Campus.
That means... You visit Bloomington.
That means... You visit Indiana.
You already visited Indiana.
Any help with implementing the visit() method?
python python-3.x
python python-3.x
asked Nov 17 '18 at 6:22
ctostainectostaine
61
61
create a global list(initially empty one). check if you have already visited the place, if not append the place into the list.
– AkshayNevrekar
Nov 17 '18 at 6:28
2
What have you tried? Why didn't it work?
– Andrew Guy
Nov 17 '18 at 7:44
add a comment |
create a global list(initially empty one). check if you have already visited the place, if not append the place into the list.
– AkshayNevrekar
Nov 17 '18 at 6:28
2
What have you tried? Why didn't it work?
– Andrew Guy
Nov 17 '18 at 7:44
create a global list(initially empty one). check if you have already visited the place, if not append the place into the list.
– AkshayNevrekar
Nov 17 '18 at 6:28
create a global list(initially empty one). check if you have already visited the place, if not append the place into the list.
– AkshayNevrekar
Nov 17 '18 at 6:28
2
2
What have you tried? Why didn't it work?
– Andrew Guy
Nov 17 '18 at 7:44
What have you tried? Why didn't it work?
– Andrew Guy
Nov 17 '18 at 7:44
add a comment |
2 Answers
2
active
oldest
votes
With python 3:
class Place:
def __init__(self, name=None, location=None):
self.name = name
self.location = location
self.visited = False
def visit(self):
if self.visited:
print(f"You already visited {self.name}.")
else:
print(f"You visit {self.name}.")
location = self.location
while location is not None:
print(f"That means... You visit {location.name}.")
location.visited = True
location = location.location
class City(Place):
def __init__(self, name, location, population, mayor):
super().__init__(name, location)
self.population = population
self.mayor = mayor
class Home(Place):
def __init__(self, name, location, num_beds, occupancy):
super().__init__(name, location)
self.num_beds = num_beds
self.occupancy = occupancy
Then when you do:
indiana = Place('Indiana')
btown = City('Bloomington', indiana, 400, 'Jim')
iu = Place('IU Campus', btown)
library = Place('Wells Library', iu)
rental = Home('Rental House', btown, 4, 3)
library.visit()
indiana.visit()
Output:
You visit Wells Library.
That means... You visit IU Campus.
That means... You visit Bloomington.
That means... You visit Indiana.
You already visited Indiana.
Thank you! This solved it for me! :-)
– ctostaine
Nov 18 '18 at 23:52
add a comment |
I have created an example and used comments to show how each piece works. I've used another subject as an example that should hopefully lead you in the right direction. This uses 1 parent class and 2 child classes. Each child has a unique function and both have replaced 1 or more functions that were already declared in the parent.
class gamePlatforms:
played = False
name = "blank"
location = "blank"
total_games = "0"
# play function that will be inherited by Console and PC classes
def play(self):
if self.played:
print("You've already played games on it.")
else:
print("First time gamer! Welcome!")
self.played = True
# print_nerdiness function that will be inherited by Console and PC classes, but will be replaced by their own
# functions (polymorphism)
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("Name of console: " + self.name)
print("Location of console: " + self.location)
print("Has it been played: " + was_played)
# set functions, good practice to create get functions as well, but I skipped that.
def set_name(self):
self.name = input("What is the name of the console: ")
def set_location(self):
self.location = input("Which room is the console in: ")
def set_game_total(self):
self.total_games = input("How many games do you have: ")
# Console: child class of gamePlatforms
class Console(gamePlatforms):
controllers = "0"
# can take 0 to 2 arguments
def __init__(self, name=None, total_games=None):
self.name = name
self.total_games = total_games
# This is a unique function for this child class
def set_controllers(self):
self.controllers = input("How many controllers does the console have: ")
# This function replaces the one from the parent class
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("-" * 20)
print("Name of console: " + self.name)
print("Amount of controllers: " + self.controllers)
print("Location of console: " + self.location)
print("Has it been played: " + was_played)
print("Amount of games: " + str(self.total_games))
# PC: child class of gamePlatforms
class PC(gamePlatforms):
OS = "blank"
# can take 0 to 2 arguments
def __init__(self, name=None, total_games=None):
self.name = name
self.total_games = total_games
# this is a unique function to this child class
def set_OS(self):
self.OS = input("What operating system does the computer have: ")
# this function replaces the parent function
def set_name(self):
self.name = input("Enter the model of the pc: ")
# this function replaces the parent function
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("-" * 20)
print("nModel of PC: " + self.name)
print("Operating system: " + self.OS)
print("Location of pc: " + self.location)
print("Has it been played: " + was_played)
print("Amount of games: " + self.total_games)
# creating a PC object, but only passing the model
myPC = PC("Dell 2000")
# creating a Console object passing Atari as the console name and total_games to 5
myConsole = Console("Atari", 5)
# Calling PC class functions to fill information, will not directly call variables outside the class object.
myPC.set_location()
myPC.set_game_total()
myPC.set_OS()
# Calling Console class functions to fill information, will not directly call variables outside the class object.
myConsole.set_location()
myConsole.set_controllers()
# Look at activity first
myPC.print_nerdiness()
myConsole.print_nerdiness()
# Time to play! This will be like your visit command
myPC.play()
myConsole.play()
# Look at activity again, it's different
myPC.print_nerdiness()
myConsole.print_nerdiness()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348793%2fpython-object-oriented%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
With python 3:
class Place:
def __init__(self, name=None, location=None):
self.name = name
self.location = location
self.visited = False
def visit(self):
if self.visited:
print(f"You already visited {self.name}.")
else:
print(f"You visit {self.name}.")
location = self.location
while location is not None:
print(f"That means... You visit {location.name}.")
location.visited = True
location = location.location
class City(Place):
def __init__(self, name, location, population, mayor):
super().__init__(name, location)
self.population = population
self.mayor = mayor
class Home(Place):
def __init__(self, name, location, num_beds, occupancy):
super().__init__(name, location)
self.num_beds = num_beds
self.occupancy = occupancy
Then when you do:
indiana = Place('Indiana')
btown = City('Bloomington', indiana, 400, 'Jim')
iu = Place('IU Campus', btown)
library = Place('Wells Library', iu)
rental = Home('Rental House', btown, 4, 3)
library.visit()
indiana.visit()
Output:
You visit Wells Library.
That means... You visit IU Campus.
That means... You visit Bloomington.
That means... You visit Indiana.
You already visited Indiana.
Thank you! This solved it for me! :-)
– ctostaine
Nov 18 '18 at 23:52
add a comment |
With python 3:
class Place:
def __init__(self, name=None, location=None):
self.name = name
self.location = location
self.visited = False
def visit(self):
if self.visited:
print(f"You already visited {self.name}.")
else:
print(f"You visit {self.name}.")
location = self.location
while location is not None:
print(f"That means... You visit {location.name}.")
location.visited = True
location = location.location
class City(Place):
def __init__(self, name, location, population, mayor):
super().__init__(name, location)
self.population = population
self.mayor = mayor
class Home(Place):
def __init__(self, name, location, num_beds, occupancy):
super().__init__(name, location)
self.num_beds = num_beds
self.occupancy = occupancy
Then when you do:
indiana = Place('Indiana')
btown = City('Bloomington', indiana, 400, 'Jim')
iu = Place('IU Campus', btown)
library = Place('Wells Library', iu)
rental = Home('Rental House', btown, 4, 3)
library.visit()
indiana.visit()
Output:
You visit Wells Library.
That means... You visit IU Campus.
That means... You visit Bloomington.
That means... You visit Indiana.
You already visited Indiana.
Thank you! This solved it for me! :-)
– ctostaine
Nov 18 '18 at 23:52
add a comment |
With python 3:
class Place:
def __init__(self, name=None, location=None):
self.name = name
self.location = location
self.visited = False
def visit(self):
if self.visited:
print(f"You already visited {self.name}.")
else:
print(f"You visit {self.name}.")
location = self.location
while location is not None:
print(f"That means... You visit {location.name}.")
location.visited = True
location = location.location
class City(Place):
def __init__(self, name, location, population, mayor):
super().__init__(name, location)
self.population = population
self.mayor = mayor
class Home(Place):
def __init__(self, name, location, num_beds, occupancy):
super().__init__(name, location)
self.num_beds = num_beds
self.occupancy = occupancy
Then when you do:
indiana = Place('Indiana')
btown = City('Bloomington', indiana, 400, 'Jim')
iu = Place('IU Campus', btown)
library = Place('Wells Library', iu)
rental = Home('Rental House', btown, 4, 3)
library.visit()
indiana.visit()
Output:
You visit Wells Library.
That means... You visit IU Campus.
That means... You visit Bloomington.
That means... You visit Indiana.
You already visited Indiana.
With python 3:
class Place:
def __init__(self, name=None, location=None):
self.name = name
self.location = location
self.visited = False
def visit(self):
if self.visited:
print(f"You already visited {self.name}.")
else:
print(f"You visit {self.name}.")
location = self.location
while location is not None:
print(f"That means... You visit {location.name}.")
location.visited = True
location = location.location
class City(Place):
def __init__(self, name, location, population, mayor):
super().__init__(name, location)
self.population = population
self.mayor = mayor
class Home(Place):
def __init__(self, name, location, num_beds, occupancy):
super().__init__(name, location)
self.num_beds = num_beds
self.occupancy = occupancy
Then when you do:
indiana = Place('Indiana')
btown = City('Bloomington', indiana, 400, 'Jim')
iu = Place('IU Campus', btown)
library = Place('Wells Library', iu)
rental = Home('Rental House', btown, 4, 3)
library.visit()
indiana.visit()
Output:
You visit Wells Library.
That means... You visit IU Campus.
That means... You visit Bloomington.
That means... You visit Indiana.
You already visited Indiana.
answered Nov 17 '18 at 6:49
SilverSlashSilverSlash
670414
670414
Thank you! This solved it for me! :-)
– ctostaine
Nov 18 '18 at 23:52
add a comment |
Thank you! This solved it for me! :-)
– ctostaine
Nov 18 '18 at 23:52
Thank you! This solved it for me! :-)
– ctostaine
Nov 18 '18 at 23:52
Thank you! This solved it for me! :-)
– ctostaine
Nov 18 '18 at 23:52
add a comment |
I have created an example and used comments to show how each piece works. I've used another subject as an example that should hopefully lead you in the right direction. This uses 1 parent class and 2 child classes. Each child has a unique function and both have replaced 1 or more functions that were already declared in the parent.
class gamePlatforms:
played = False
name = "blank"
location = "blank"
total_games = "0"
# play function that will be inherited by Console and PC classes
def play(self):
if self.played:
print("You've already played games on it.")
else:
print("First time gamer! Welcome!")
self.played = True
# print_nerdiness function that will be inherited by Console and PC classes, but will be replaced by their own
# functions (polymorphism)
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("Name of console: " + self.name)
print("Location of console: " + self.location)
print("Has it been played: " + was_played)
# set functions, good practice to create get functions as well, but I skipped that.
def set_name(self):
self.name = input("What is the name of the console: ")
def set_location(self):
self.location = input("Which room is the console in: ")
def set_game_total(self):
self.total_games = input("How many games do you have: ")
# Console: child class of gamePlatforms
class Console(gamePlatforms):
controllers = "0"
# can take 0 to 2 arguments
def __init__(self, name=None, total_games=None):
self.name = name
self.total_games = total_games
# This is a unique function for this child class
def set_controllers(self):
self.controllers = input("How many controllers does the console have: ")
# This function replaces the one from the parent class
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("-" * 20)
print("Name of console: " + self.name)
print("Amount of controllers: " + self.controllers)
print("Location of console: " + self.location)
print("Has it been played: " + was_played)
print("Amount of games: " + str(self.total_games))
# PC: child class of gamePlatforms
class PC(gamePlatforms):
OS = "blank"
# can take 0 to 2 arguments
def __init__(self, name=None, total_games=None):
self.name = name
self.total_games = total_games
# this is a unique function to this child class
def set_OS(self):
self.OS = input("What operating system does the computer have: ")
# this function replaces the parent function
def set_name(self):
self.name = input("Enter the model of the pc: ")
# this function replaces the parent function
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("-" * 20)
print("nModel of PC: " + self.name)
print("Operating system: " + self.OS)
print("Location of pc: " + self.location)
print("Has it been played: " + was_played)
print("Amount of games: " + self.total_games)
# creating a PC object, but only passing the model
myPC = PC("Dell 2000")
# creating a Console object passing Atari as the console name and total_games to 5
myConsole = Console("Atari", 5)
# Calling PC class functions to fill information, will not directly call variables outside the class object.
myPC.set_location()
myPC.set_game_total()
myPC.set_OS()
# Calling Console class functions to fill information, will not directly call variables outside the class object.
myConsole.set_location()
myConsole.set_controllers()
# Look at activity first
myPC.print_nerdiness()
myConsole.print_nerdiness()
# Time to play! This will be like your visit command
myPC.play()
myConsole.play()
# Look at activity again, it's different
myPC.print_nerdiness()
myConsole.print_nerdiness()
add a comment |
I have created an example and used comments to show how each piece works. I've used another subject as an example that should hopefully lead you in the right direction. This uses 1 parent class and 2 child classes. Each child has a unique function and both have replaced 1 or more functions that were already declared in the parent.
class gamePlatforms:
played = False
name = "blank"
location = "blank"
total_games = "0"
# play function that will be inherited by Console and PC classes
def play(self):
if self.played:
print("You've already played games on it.")
else:
print("First time gamer! Welcome!")
self.played = True
# print_nerdiness function that will be inherited by Console and PC classes, but will be replaced by their own
# functions (polymorphism)
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("Name of console: " + self.name)
print("Location of console: " + self.location)
print("Has it been played: " + was_played)
# set functions, good practice to create get functions as well, but I skipped that.
def set_name(self):
self.name = input("What is the name of the console: ")
def set_location(self):
self.location = input("Which room is the console in: ")
def set_game_total(self):
self.total_games = input("How many games do you have: ")
# Console: child class of gamePlatforms
class Console(gamePlatforms):
controllers = "0"
# can take 0 to 2 arguments
def __init__(self, name=None, total_games=None):
self.name = name
self.total_games = total_games
# This is a unique function for this child class
def set_controllers(self):
self.controllers = input("How many controllers does the console have: ")
# This function replaces the one from the parent class
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("-" * 20)
print("Name of console: " + self.name)
print("Amount of controllers: " + self.controllers)
print("Location of console: " + self.location)
print("Has it been played: " + was_played)
print("Amount of games: " + str(self.total_games))
# PC: child class of gamePlatforms
class PC(gamePlatforms):
OS = "blank"
# can take 0 to 2 arguments
def __init__(self, name=None, total_games=None):
self.name = name
self.total_games = total_games
# this is a unique function to this child class
def set_OS(self):
self.OS = input("What operating system does the computer have: ")
# this function replaces the parent function
def set_name(self):
self.name = input("Enter the model of the pc: ")
# this function replaces the parent function
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("-" * 20)
print("nModel of PC: " + self.name)
print("Operating system: " + self.OS)
print("Location of pc: " + self.location)
print("Has it been played: " + was_played)
print("Amount of games: " + self.total_games)
# creating a PC object, but only passing the model
myPC = PC("Dell 2000")
# creating a Console object passing Atari as the console name and total_games to 5
myConsole = Console("Atari", 5)
# Calling PC class functions to fill information, will not directly call variables outside the class object.
myPC.set_location()
myPC.set_game_total()
myPC.set_OS()
# Calling Console class functions to fill information, will not directly call variables outside the class object.
myConsole.set_location()
myConsole.set_controllers()
# Look at activity first
myPC.print_nerdiness()
myConsole.print_nerdiness()
# Time to play! This will be like your visit command
myPC.play()
myConsole.play()
# Look at activity again, it's different
myPC.print_nerdiness()
myConsole.print_nerdiness()
add a comment |
I have created an example and used comments to show how each piece works. I've used another subject as an example that should hopefully lead you in the right direction. This uses 1 parent class and 2 child classes. Each child has a unique function and both have replaced 1 or more functions that were already declared in the parent.
class gamePlatforms:
played = False
name = "blank"
location = "blank"
total_games = "0"
# play function that will be inherited by Console and PC classes
def play(self):
if self.played:
print("You've already played games on it.")
else:
print("First time gamer! Welcome!")
self.played = True
# print_nerdiness function that will be inherited by Console and PC classes, but will be replaced by their own
# functions (polymorphism)
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("Name of console: " + self.name)
print("Location of console: " + self.location)
print("Has it been played: " + was_played)
# set functions, good practice to create get functions as well, but I skipped that.
def set_name(self):
self.name = input("What is the name of the console: ")
def set_location(self):
self.location = input("Which room is the console in: ")
def set_game_total(self):
self.total_games = input("How many games do you have: ")
# Console: child class of gamePlatforms
class Console(gamePlatforms):
controllers = "0"
# can take 0 to 2 arguments
def __init__(self, name=None, total_games=None):
self.name = name
self.total_games = total_games
# This is a unique function for this child class
def set_controllers(self):
self.controllers = input("How many controllers does the console have: ")
# This function replaces the one from the parent class
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("-" * 20)
print("Name of console: " + self.name)
print("Amount of controllers: " + self.controllers)
print("Location of console: " + self.location)
print("Has it been played: " + was_played)
print("Amount of games: " + str(self.total_games))
# PC: child class of gamePlatforms
class PC(gamePlatforms):
OS = "blank"
# can take 0 to 2 arguments
def __init__(self, name=None, total_games=None):
self.name = name
self.total_games = total_games
# this is a unique function to this child class
def set_OS(self):
self.OS = input("What operating system does the computer have: ")
# this function replaces the parent function
def set_name(self):
self.name = input("Enter the model of the pc: ")
# this function replaces the parent function
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("-" * 20)
print("nModel of PC: " + self.name)
print("Operating system: " + self.OS)
print("Location of pc: " + self.location)
print("Has it been played: " + was_played)
print("Amount of games: " + self.total_games)
# creating a PC object, but only passing the model
myPC = PC("Dell 2000")
# creating a Console object passing Atari as the console name and total_games to 5
myConsole = Console("Atari", 5)
# Calling PC class functions to fill information, will not directly call variables outside the class object.
myPC.set_location()
myPC.set_game_total()
myPC.set_OS()
# Calling Console class functions to fill information, will not directly call variables outside the class object.
myConsole.set_location()
myConsole.set_controllers()
# Look at activity first
myPC.print_nerdiness()
myConsole.print_nerdiness()
# Time to play! This will be like your visit command
myPC.play()
myConsole.play()
# Look at activity again, it's different
myPC.print_nerdiness()
myConsole.print_nerdiness()
I have created an example and used comments to show how each piece works. I've used another subject as an example that should hopefully lead you in the right direction. This uses 1 parent class and 2 child classes. Each child has a unique function and both have replaced 1 or more functions that were already declared in the parent.
class gamePlatforms:
played = False
name = "blank"
location = "blank"
total_games = "0"
# play function that will be inherited by Console and PC classes
def play(self):
if self.played:
print("You've already played games on it.")
else:
print("First time gamer! Welcome!")
self.played = True
# print_nerdiness function that will be inherited by Console and PC classes, but will be replaced by their own
# functions (polymorphism)
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("Name of console: " + self.name)
print("Location of console: " + self.location)
print("Has it been played: " + was_played)
# set functions, good practice to create get functions as well, but I skipped that.
def set_name(self):
self.name = input("What is the name of the console: ")
def set_location(self):
self.location = input("Which room is the console in: ")
def set_game_total(self):
self.total_games = input("How many games do you have: ")
# Console: child class of gamePlatforms
class Console(gamePlatforms):
controllers = "0"
# can take 0 to 2 arguments
def __init__(self, name=None, total_games=None):
self.name = name
self.total_games = total_games
# This is a unique function for this child class
def set_controllers(self):
self.controllers = input("How many controllers does the console have: ")
# This function replaces the one from the parent class
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("-" * 20)
print("Name of console: " + self.name)
print("Amount of controllers: " + self.controllers)
print("Location of console: " + self.location)
print("Has it been played: " + was_played)
print("Amount of games: " + str(self.total_games))
# PC: child class of gamePlatforms
class PC(gamePlatforms):
OS = "blank"
# can take 0 to 2 arguments
def __init__(self, name=None, total_games=None):
self.name = name
self.total_games = total_games
# this is a unique function to this child class
def set_OS(self):
self.OS = input("What operating system does the computer have: ")
# this function replaces the parent function
def set_name(self):
self.name = input("Enter the model of the pc: ")
# this function replaces the parent function
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("-" * 20)
print("nModel of PC: " + self.name)
print("Operating system: " + self.OS)
print("Location of pc: " + self.location)
print("Has it been played: " + was_played)
print("Amount of games: " + self.total_games)
# creating a PC object, but only passing the model
myPC = PC("Dell 2000")
# creating a Console object passing Atari as the console name and total_games to 5
myConsole = Console("Atari", 5)
# Calling PC class functions to fill information, will not directly call variables outside the class object.
myPC.set_location()
myPC.set_game_total()
myPC.set_OS()
# Calling Console class functions to fill information, will not directly call variables outside the class object.
myConsole.set_location()
myConsole.set_controllers()
# Look at activity first
myPC.print_nerdiness()
myConsole.print_nerdiness()
# Time to play! This will be like your visit command
myPC.play()
myConsole.play()
# Look at activity again, it's different
myPC.print_nerdiness()
myConsole.print_nerdiness()
answered Nov 17 '18 at 7:40
Jessia AngelaJessia Angela
13
13
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348793%2fpython-object-oriented%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
create a global list(initially empty one). check if you have already visited the place, if not append the place into the list.
– AkshayNevrekar
Nov 17 '18 at 6:28
2
What have you tried? Why didn't it work?
– Andrew Guy
Nov 17 '18 at 7:44