Challenge
Compito
Swipe to start coding
- Create the
Cat
class:
- Set
info
andmake_sound
methods inside theCat
class; - Inside the
make_sound
method print'Meow'
.
- Create the
Dog
class:
- Set
info
andmake_sound
methods inside theDog
class; - Inside the
make_sound
method print'Bark'
.
- Create
cat
anddog
objects based on theCat
andDog
classes respectively.
Soluzione
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Create the Cat class
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
# Set the info method
def info(self):
print('I am a cat. My name is', self.name, 'I am', self.age, 'years old')
# Set the make_sound method
def make_sound(self):
# Print 'Meow'
print('Meow')
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
# Set the info method
def info(self):
print('I am a dog. My name is', self.name, 'I am', self.age, 'years old')
# Set the make_sound method
def make_sound(self):
# Print 'Bark'
print('Bark')
# Create the cat object
cat = Cat('Kitty', 2)
cat.info()
cat.make_sound()
# Create the dog object
dog = Dog('Fluffy', 4)
dog.info()
dog.make_sound()
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 4. Capitolo 2
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Create the Cat class
class ___:
def __init__(self, name, age):
self.name = name
self.age = age
# Set the info method
___(self):
print('I am a cat. My name is', self.name, 'I am', self.age, 'years old')
# Set the make_sound method
___(self):
# Print 'Meow'
___('___')
___:
def __init__(self, name, age):
self.name = name
self.age = age
# Set the info method
___
print('I am a dog. My name is', self.name, 'I am', self.age, 'years old')
# Set the make_sound method
___
# Print 'Bark'
___
# Create the cat object
___ = ___('Kitty', 2)
cat.info()
cat.make_sound()
# Create the dog object
___'Fluffy', 4)
dog.info()
dog.make_sound()
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione