1
1
import random
2
2
3
3
class Animal :
4
- """a generic animal"""
4
+ """a generic animal"""
5
5
6
- #constructor
7
- def __init__ (self ,growth_rate ,food_need ,water_need ,name ):
8
- #set the attributes with an initial value
6
+ #constructor
7
+ def __init__ (self ,growth_rate ,food_need ,water_need ,name ):
8
+ #set the attributes with an initial value
9
9
10
- self ._weight = 0
11
- self ._days_growing = 0
12
- self ._growth_rate = growth_rate
13
- self ._food_need = food_need
14
- self ._water_need = water_need
15
- self ._status = "Baby"
16
- self ._type = "Generic"
17
- self .name = name
10
+ self ._weight = 0
11
+ self ._days_growing = 0
12
+ self ._growth_rate = growth_rate
13
+ self ._food_need = food_need
14
+ self ._water_need = water_need
15
+ self ._status = "Baby"
16
+ self ._type = "Generic"
17
+ self .name = name
18
18
19
- #the above attributes are prefixed with an underscore to indicate that they should not be
20
- #accessed directly from outwith the class except for name which can be changed freely
19
+ #the above attributes are prefixed with an underscore to indicate that they should not be
20
+ #accessed directly from outwith the class except for name which can be changed freely
21
21
22
- #method to indicate the needs of the animal
23
- def needs (self ):
24
- #return a dictionary containing the food and water needs
25
- return {'food need' :self ._food_need ,'water need' :self ._water_need }
22
+ #method to indicate the needs of the animal
23
+ def needs (self ):
24
+ #return a dictionary containing the food and water needs
25
+ return {'food need' :self ._food_need ,'water need' :self ._water_need }
26
26
27
- #method to provide information about the current state of the animal
28
- def report (self ):
29
- #return a dictionary containing name,type,status, weight and days growing
30
- return {'name' :self .name ,'type' :self ._type ,'status' :self ._status ,'weight' :self ._weight ,'days growing' :self ._days_growing }
27
+ #method to provide information about the current state of the animal
28
+ def report (self ):
29
+ #return a dictionary containing name,type,status, weight and days growing
30
+ return {'name' :self .name ,'type' :self ._type ,'status' :self ._status ,'weight' :self ._weight ,'days growing' :self ._days_growing }
31
31
32
- #the underscore indicates that this method should not be called from outwith the class
33
- def _update_status (self ):
34
- if self ._weight > 30 :
35
- self ._status = "Prime"
36
- elif self ._weight > 15 :
37
- self ._status = "Fine"
38
- elif self ._weight > 10 :
39
- self ._status = "Poor"
40
- elif self ._weight >= 0 :
41
- self ._status = "Baby"
32
+ #the underscore indicates that this method should not be called from outwith the class
33
+ def _update_status (self ):
34
+ if self ._weight > 30 :
35
+ self ._status = "Prime"
36
+ elif self ._weight > 15 :
37
+ self ._status = "Fine"
38
+ elif self ._weight > 10 :
39
+ self ._status = "Poor"
40
+ elif self ._weight >= 0 :
41
+ self ._status = "Baby"
42
42
43
- def grow (self ,food ,water ):
44
- if food >= self ._food_need and water >= self ._water_need :
45
- self ._weight += self ._growth_rate
46
- #increment days growing
47
- self ._days_growing += 1
48
- #update the status
49
- self ._update_status ()
43
+ def grow (self ,food ,water ):
44
+ if food >= self ._food_need and water >= self ._water_need :
45
+ self ._weight += self ._growth_rate
46
+ #increment days growing
47
+ self ._days_growing += 1
48
+ #update the status
49
+ self ._update_status ()
50
50
51
51
def manual_grow (self ):
52
- #get the food and water values from the user
53
- valid = False
54
- while not valid :
55
- try :
56
- food = int (input ("Please enter a food value (1-10): " ))
57
- if 1 <= food <= 10 :
58
- valid = True
59
- else :
60
- print ("Value entered not valid - please enter a value between 1 and 10" )
61
- except ValueError :
62
- print ("Value entered not valid - please enter a value between 1 and 10" )
63
- valid = False
64
- while not valid :
65
- try :
66
- water = int (input ("Please enter a water value (1-10): " ))
67
- if 1 <= water <= 10 :
68
- valid = True
69
- else :
70
- print ("Value entered not valid - please enter a value between 1 and 10" )
71
- except ValueError :
72
- print ("Value entered not valid - please enter a value between 1 and 10" )
73
- #grow the animal
74
- self .grow (food ,water )
52
+ #get the food and water values from the user
53
+ valid = False
54
+ while not valid :
55
+ try :
56
+ food = int (input ("Please enter a food value (1-10): " ))
57
+ if 1 <= food <= 10 :
58
+ valid = True
59
+ else :
60
+ print ("Value entered not valid - please enter a value between 1 and 10" )
61
+ except ValueError :
62
+ print ("Value entered not valid - please enter a value between 1 and 10" )
63
+ valid = False
64
+ while not valid :
65
+ try :
66
+ water = int (input ("Please enter a water value (1-10): " ))
67
+ if 1 <= water <= 10 :
68
+ valid = True
69
+ else :
70
+ print ("Value entered not valid - please enter a value between 1 and 10" )
71
+ except ValueError :
72
+ print ("Value entered not valid - please enter a value between 1 and 10" )
73
+ #grow the animal
74
+ self .grow (food ,water )
75
75
76
76
def auto_grow (self ,days ):
77
- #grow the animal automatically over 30 days
78
- for day in range (days ):
79
- food = random .randint (1 ,10 )
80
- water = random .randint (1 ,10 )
81
- self .grow (food ,water )
77
+ #grow the animal automatically over 30 days
78
+ for day in range (days ):
79
+ food = random .randint (1 ,10 )
80
+ water = random .randint (1 ,10 )
81
+ self .grow (food ,water )
82
82
83
83
def displayMenu ():
84
84
print ('1. Grow manually over 1 day' )
@@ -102,32 +102,32 @@ def getMenuChoice():
102
102
return choice
103
103
104
104
def manage_animal (self ):
105
- print ('This is animal management program' )
106
- print ()
107
- noexit = True
108
- while noexit :
109
- displayMenu ()
110
- option = getMenuChoice ()
111
- print ()
112
- if option == 1 :
113
- manual_grow (self )
114
- print ()
115
- elif option == 2 :
116
- auto_grow (self ,30 )
117
- print ()
118
- elif option == 3 :
119
- print (self .report ())
120
- print ()
121
- elif option == 0 :
122
- noexit = False
123
- print ()
124
- print ('Thank you for using the animal management program' )
105
+ print ('This is animal management program' )
106
+ print ()
107
+ noexit = True
108
+ while noexit :
109
+ displayMenu ()
110
+ option = getMenuChoice ()
111
+ print ()
112
+ if option == 1 :
113
+ manual_grow (self )
114
+ print ()
115
+ elif option == 2 :
116
+ auto_grow (self ,30 )
117
+ print ()
118
+ elif option == 3 :
119
+ print (self .report ())
120
+ print ()
121
+ elif option == 0 :
122
+ noexit = False
123
+ print ()
124
+ print ('Thank you for using the animal management program' )
125
125
126
126
def main ():
127
- #instaniate the class
128
- new_animal = Animal (1 ,4 ,5 ,"Sally" )
129
- #test to see whether it works or not
130
- manage_animal (new_animal )
127
+ #instaniate the class
128
+ new_animal = Animal (1 ,4 ,5 ,"Sally" )
129
+ #test to see whether it works or not
130
+ manage_animal (new_animal )
131
131
132
132
if __name__ == '__main__' :
133
- main ()
133
+ main ()
0 commit comments