0 ratings0% found this document useful (0 votes) 14 views2 pagesWeek 1 Python
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Instantly share code, notes, and snippets
AnOnYm0us001100 / course_4_assessment_1.py
Created 3 years ago
WY Star
© Code © Revisions 1
‘course_4_assessment_1 of Python Classes and Inheritance under Python 3 Specialization offered by Coursera
El course 4 assessuent apy
10
a
2
a
Pa
6
16
wv
18
»
20
a
2
23
24
2s
26
a
28
29
30
31
22
#1/usr/oin/env python
# coding: utf-8
1. Define a class called Bike that accepts a string and a float as input, and assigns those inputs re
to two instance variables, color and price. Assign to the variable testOne an instance of Bike whose
blue and whose price is 89.99. Assign to the variable testTwo an instance of Bike whose color is purp
whose price is 25.0.
class ike
def _init_(self,st,1)
[Link] = st
[Link] = fl
testone = Bike("blue”,89.99)
testIwo = Bike("purple",25.0)
print (testone)
print (testTwo)
2, Create a class called AppleBasket whose constructor accepts tho inputs: a string representing a co
land a nunber representing a quantity of apples. The constructor should initialize tio instance variab
apple_color and apple_quantity. Write 2 class method called increase that increases the quantity by 1
time ie Ls Invoked. You should also write a _str__ method for this class that returns a string of th
‘A basket of [quantity goes here] [color goes here] apples." e.g. “A basket of 4 red apples." or "Ab
5@ blue apples." (Writing sone test code that creates instances and assigns values to variables may h
solve this problen!)
class AppleBasket
def _init_(self,c,a)
self.apple_color = ¢33
34
35
36
7
38
39
40
a
a2
a
aa
45
46
a7
48
49
50
st
52
53
sa
55
56
s7
58
59
self.apple_quantity = q
def increase(self):
self.apple_quantity ¢= 1
def _str_(self):
return "A basket of {} {} apples.".fornat(self.apple_quantity, self. apple_color)
a = Applefasket (“red*,4)
print (a)
3. Define a class called BankAccount that accepts the name you want associated with your bank account
string, and an integer that represents the anount of money in the account. The constructor should ini
two instance variables fron those inputs: name and amt. Add a string method so that when you print an
of BankAccount, you see “Your account, [name goes here], has [start_ant goes here] dollars
create an instance of this class with "Bob" as the name and 100 as the amount. Save this to the varia
class SankAccount
def _init__(self,name,ant)
[Link] = name
[Link] = ant
def _str_(sel#):
return "Your account, {}, has {} dollars.".fornat([Link], self ant)
tL = BankAccount(“Bob",120)
print (t1)