-
Notifications
You must be signed in to change notification settings - Fork 22
/
Car.py
39 lines (37 loc) · 1.11 KB
/
Car.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
class Car():
#constructor
def __init__(self, model='Ford', wheels=4):
#setting defaults
self.model = model
self.running = False
self.wheels = wheels
#if wheels is negative set to zero
if self.wheels < 0:
print "You can't have negative wheels on a car."
self.wheels = 0
#function to start car
def start(self):
if self.running != True:
print 'The car started!'
self.running = True
else:
print 'The car is already running!'
#function to stop car
def stop(self):
if self.running == True:
print 'The car stopped!'
self.running = False
else:
print 'The car was not running!'
#Sample class variable assignments and function calls
ford = Car()
nissan = Car(model = 'Nissan')
ford.running
ford.start() #The car started!
ford.running
nissan.running
nissan.stop() #The car was not running!
merc = Car('Mercedes', -2) #You can't have negative wheels on a car.
merc.start() #The car started!
bent = Car('Bentley')
bent.start() #The car started!