Parsons Practice Problems¶
Please answer the following problems to the best of your ability without any outside help. You can stop working on a problem after you worked on it for about five minutes without solving it.
Problems¶
Create a class Song
with an __init__
method that takes a title
as a string and len
as a number and initializes these attributes in the current object.
Then define the __str__
method to return the title, len
. For example, print(s)
when s = Song('Respect',150)
would print “Respect, 150”.
Create a class Cat with an __init__
method that takes
name
as a string and age
as a number and initializes these attributes in the current object. Next create the __str__
method that returns
“name: name age: age”. For example if c = Cat("Fluffy", 3)
then
print(c)
should print "name: Fluffy age: 3"
.
Then define the make_sound
method to return "Meow"
.
Create a class Account
with an __init__
method that takes id
and balance
as numbers. Then create a __str__
method that returns “id, balance”. Next create a deposit
method takes amount
as a number and adds that to the balance
. For example,
if a = Account(32, 100)
and a.deposit(50)
is executed, print(a)
should print “32, 150”.
Create a class FortuneTeller
with an __init__
method that takes a list of fortunes as strings and saves that as an attribute. Then create a tell_fortune
method that returns one of the fortunes in the list at random.