#Week 10, Ch 14, Ex 1 #Program creates an object of the class and prompts the user to enter the name, #type, and age of pet. Data is stored in the object and uses accessor methods to retrieve #the pet’s name, type, and age and displays it on the screen. #Written by Greg Lamb #Last modified 5/16/2015 # Create class for Pet class Pet: __name = "" __species = "" __age = "" # Object constructor def __init__(self, name, species, age): self.__name = name self.__species = species self.__age = age #Methods def setName(self, name): self.__name = name def setSpecies(self, species): self.__species = species def setAge(self, age): self.__age = age def getName(self): return self.__name def getSpecies(self): return self.__species def getAge(self): return self.__age def get_type(self): print("Pet") def toString(self):#access return"{} is a {} and is {} years old".format(self.__name,self.__species,self.__age) #User input name=input("What is your pet's name?\n") species=input("What type of pet is it?\n") age=input("How old is your pet?\n") #Call object from class attributes myPet=Pet(name,species,age) print(myPet.toString())
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question