forked from DrSkippy/Data-Science-45min-Intros
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_module.py
executable file
·85 lines (69 loc) · 2.01 KB
/
simple_module.py
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__="Josh Montague"
__license__="MIT License"
import sys
######################
# part 1
#
my_int = 4
my_s = "hello world!"
def square(number):
"""Return the squared value of 'number'."""
try:
return number**2
except TypeError as e:
sys.stderr.write("Can't square something that's not a number! ({})".format(e))
######################
# part 2
#
class Dog(object):
"""Create a general Dog object. Takes no arguments."""
def __init__(self):
self.name = "rex"
self.legs = 4
self.owner = "jane"
self.word = "woof"
def talk(self):
"""Return a statement about the attributes of this Dog."""
s = "{}, my name is {}. i have {} legs and belong to {}.".format(
self.word
, self.name
, self.legs
, self.owner
)
return s
######################
# part 3
#
class Cat(object):
"""
Create a general Cat object. Requires a name and optional count of legs
and owner name.
"""
def __init__(self, name, legs=4, owner="john"):
self.name = name
self.legs = legs # note that we're not doing any type checking...
self.owner = owner
self.word = "meow"
def talk(self):
"""Return a statement about the attributes of this Dog."""
s = "{}, my name is {}. i have {} legs and belong to {}.".format(
self.word
, self.name
, self.legs
, self.owner
)
return s
##################################################
if __name__ == '__main__':
print # cheap carriage returns in stdout
sys.stdout.write("Now creating a Cat named Sue, with 84 legs, belonging to Jeff.")
sys.stdout.write(".\n"*10)
c = Cat("Sue", legs=84, owner="jeff")
sys.stdout.write("SPEAK, CAT!")
print
print
sys.stdout.write('"' + c.talk() + '"')
print
print