-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathanimals.py
More file actions
110 lines (84 loc) · 2.66 KB
/
animals.py
File metadata and controls
110 lines (84 loc) · 2.66 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 8 14:27:04 2020
@author: Kirby Swallows Sans
"""
import random
class Animal:
pass
class Mammal(Animal):
"""
Demonstrates the use of special names
"""
def __init__(self, nm, how_old):
self.name = nm
self.age = how_old
self.stomach = [ ]
def eat(self, food):
self.stomach.append(food)
def __call__(self, food):
# lets you feed an animal like dog("food")
self.eat(food)
def __getitem__(self, num):
# changes how [ ] works
return self.stomach[num]
def __add__(self, other):
# operator overloading!
return self.name + " meets " + other.name
def barf(self):
contents = " ".join(self.stomach)
self.stomach = [ ]
return contents
class Cat(Mammal):
def meow(self, n=1):
return "meow!" * n
def __repr__(self):
return 'Cat("{}", {})'.format(self.name, self.age)
class Dog(Mammal):
def bark(self, n=1):
return "bark!" * n
def __repr__(self):
return 'Dog("{}", {})'.format(self.name, self.age)
class Platypus(Mammal):
def quack(self, n=1):
return "quack!" * n
def __repr__(self):
return 'Platypus("{}", {})'.format(self.name, self.age)
def test_dog():
doggie = Dog("Rover", 3)
foods = """
🍄 🍅 🍆 🍇 🍈 🍉 🍊 🍌 🍍 🍎
🍏 🍐 🍑 🍒 🍓 🍔 🍕 🍗 🍘 🍙
🍚 🍛 🍜 🍝 🍞 🍟 🍠 🍢 🍣 🍤
🍥 🍦 🍧 🍨 🍩 🍪 🍫 🍭 🍮 🍯
🍰
""".split()
# eat between one and five things
meal = random.choices(foods, k=random.randint(1, 5))
print("Eating a meal of {} things".format(len(meal)))
for f in meal:
doggie.eat(f)
# oh oh, something doggie ate was unsuitable
print("doggie doesn't feel well.")
print(doggie.barf())
def test_cat():
cat = Cat("Felix", 2)
foods = """
🍄 🍅 🍆 🍇 🍈 🍉 🍊 🍌 🍍 🍎
🍏 🍐 🍑 🍒 🍓 🍔 🍕 🍗 🍘 🍙
🍚 🍛 🍜 🍝 🍞 🍟 🍠 🍢 🍣 🍤
🍥 🍦 🍧 🍨 🍩 🍪 🍫 🍭 🍮 🍯
🍰
""".split()
# eat between one and five things
meal = random.choices(foods, k=random.randint(1, 5))
print("Eating a meal of {} things".format(len(meal)))
for f in meal:
cat.eat(f)
# oh oh, something doggie ate was unsuitable
print("doggie doesn't feel well.")
print(cat.barf())
if __name__ == "__main__":
test_dog()
test_cat()