# Chapter 14 Exercise 1 # This program uses a Class named Pet to create an object of the class and prompts the user to enter the name, # type, and age of his or her pet. This data is stored in the object and then is used to retrieve the pet's # name, type, and age, and displays it on the screen. # Written by Beth Mela # Last updated June 4, 2015 # Define Pet Class with features: class Pet: name="" species="" age="" # Constructor def __init__(self, name, species, age): self.name= name self.species= species self.age= age def convertToString(self): return'{} is a {} and is {} years old'.format(self.name,self.species,self.age) # Get input from user: name=raw_input("What is your pet's name?\n") species=raw_input("What species is your pet?\n") age=raw_input("How many years old is your pet?\n") # Display user entered data: myPet=Pet(name,species,age) print(myPet.convertToString())
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