Mixed-Up Code Exercises¶
Create a class Airport
with an __init__
method that takes a name
and code
as strings and initializes these attributes in the current object.
Then define the __str__
method to return the name code
. For example, print(a)
when a = Airport("Detroit", "DTW")
would print Detroit DTW
.
Write a class Airport
with an __init__
method that takes a name
and code
as strings and initializes these attributes in the current object.
Then define the __str__
method to return the name code
. For example, print(a)
when a = Airport("Detroit", "DTW")
would print Detroit DTW
.
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”.
Write 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"
.
Write 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 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.
Write 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.
Create a class Student
with an __init__
method that takes a student’s name as a string and a list of exam scores as integers. Then create a __str__
method that returns the “name: average exam score” rounded to the nearest integer. For example, print(s)
when s = Student("Sarah", [91,92,97])
would print Sarah: 93.0
.
Write a class Student
with an __init__
method that takes a student’s name as a string and a list of exam scores as integers. Then create a __str__
method that returns the “name: average exam score” rounded to the nearest integer. For example, print(s)
when s = Student("Sarah", [91,92,97])
would print Sarah: 93.0
.
Create a class Encoder
with an initializer method that takes in a sentence as a string. Then create a string method that returns “string: length of sentence”. Lastly, create a consonants method that returns the sentence without vowels, all lowercase. For example, print(e)
when e = Encoder("I love Python")
would print I love Python: 13
and print(e.consonants())
would print `` lv pythn``.
Write a class Encoder
with an initializer method that takes in a sentence as a string. Then create a string method that returns “string: length of sentence”. Lastly, create a consonants method that returns the sentence without vowels, all lowercase. For example, print(e)
when e = Encoder("I love Python")
would print I love Python: 13
and print(e.consonants())
would print `` lv pythn``.
Create a class CharFrequency
with an initializer method that takes in a sentence as a string. Then create a string method that returns “The most common character in the sentence is x.” In the case of tiebreakers, return the character that comes last in alphabetical order.
For example, print(c)
when c = CharFreequency("I love Python")
would print The most common character in the sentence is o.
Write a class CharFrequency
with an initializer method that takes in a sentence as a string. Then create a string method that returns “The most common character in the sentence is x.” In the case of tiebreakers, return the character that comes last in alphabetical order. For example, print(c)
when c = CharFreequency("I love Python")
would print The most common character in the sentence is o.
Create a class Loan
with an initializer method that takes in an integer loan_amt, a float interest_rate and integer years. Then create a string method that returns “Your loan is ‘’loan_amt’’ for ‘’years’’ years at an interest rate of ‘’interest_rate’’.” Create another method called total_payment that calculates the total amount of loan payment based on the interest rate and number of years. Use the formula (loan_amt * (1 + interest_rate/100) ^ years). Also round your total payment to two decimal places. We’re assuming that you are paying on an annual basis. For example, print(l)
when l = Loan(10000, 5, 10)
would print Your loan is $10000 for 10 years at an interest rate of 5%.
and print(l.total_payment())
would print 16288.95
as a float.
Write a class Loan
with an initializer method that takes in an integer loan_amt, a float interest_rate and integer years. Then write a string method that returns “Your loan is ‘’loan_amt’’ for ‘’years’’ years at an interest rate of ‘’interest_rate’’.” Write another method called total_payment that calculates the total amount of loan payment based on the interest rate and number of years. Use the formula (loan_amt * (1 + interest_rate/100) ^ years). Also round your total payment to two decimal places. We’re assuming that you are paying on an annual basis. For example, print(l)
when l = Loan(10000, 5, 10)
would print Your loan is $10000 for 10 years at an interest rate of 5%.
and print(l.total_payment())
would print 16288.95
as a float.
Create a class Cart
that takes in a dictionary price_dict with keys as product names and values as the product price. The initializer method also creates a new empty list called cart_list that will contain tuples in the format (item, quantity). The class also has an add_item and a calculate_total method. The add_item method will take in an item and quantity you wish to purchase and append those to the cart. The calculate_total method should calculate and return the total amount of your purchase for all of the items in your cart. For example, when c = Cart({“Notebook”: 1.99, “Pen”: 2.50, “Paper”: 1.00})
and a user wants to purchase 3 pens by calling c.add_item(“Pen”, 3), then print(c.calculate_total) would print 7.50
as a float.
Create a class Cart
that takes in a dictionary price_dict with keys as product names and values as the product price. The initializer method also creates a new empty list called cart_list that will contain tuples in the format (item, quantity). The class also has an add_item and a calculate_total method. The add_item method will take in an item and quantity you wish to purchase and append those to the cart. The calculate_total method should calculate and return the total amount of your purchase for all of the items in your cart. For example, when c = Cart({“Notebook”: 1.99, “Pen”: 2.50, “Paper”: 1.00})
and a user wants to purchase 3 pens by calling c.add_item(“Pen”, 3), then print(c.calculate_total) would print 7.50
as a float.