# Extra Exercises¶

1. Create a class called Bike that contains two instance variables, color and price. Assign to the variable testOne, an instance of Bike whose color is blue and price is 89.99. Assign to the variable testTwo, an instance of Bike whose color is purple and price is 25.

1.1 Create a class called inst_var that defines two instance variables: num1 and num2. Then, create an instance where num1 is 6 and num2 is 10 and save this instance to the variable t.

1.2 Create a class called House that has three instance variables: color, rooms, and price. To the variable name houseOne, assign an instance of House whose color is white, has 3 rooms, and costs 50000. To the variable name houseTwo, assign an instance of House whose color is red, has 9 rooms, and costs 1000000.

1. Create a class called math_op with one instance variable and a method. The instance variable should be numb. The method should be called squared and return the instance variable squared. Create an instance of this class with an initial number of 8. Assign to the variable output the value of numb without hardcoding. Call the method so that the value is 64.

2.1 Create a class called Apple that contains one instance variable, quantity. Write a class method called increase that increases the quantity by 1 each time it is run. Assign to the variable tester, an instance of Apple that has an initial quantity of 4. Assign to the variable initial_quantity the value of tester’s quantity without hardcoding. Call the method four times.

2.2 Create a class called Animal that has two instance variables: arms and legs. Create a class method called limbs that, when called, returns the total number of limbs the animal has. To the variable name spider, assign an instance of Animal that has 4 arms and 4 legs. Call the limbs method on spider and save the result to the variable name spidlimbs.

1. Create a class called bank that contains two instance variables, name and amt. Add the instance method that allows you to customize the message returned when you print the instance so that it says “Your account, [name goes here], has [start_amt goes here] dollars.” Create an instance of this class with “Bob” as the name and 100 as the amount. Save this to the variable t1.

3.1 Create a class called Sports that contains 2 instance variables, name and number_of_players. Add the instance method that allows you to customize the message returned when you print the instance so that it says “The name of this sport is [name goes here] and [number_of_players goes here] people create one team!” Create two instances of the class, one assigned to the variable football_info and one called quidditch_info. The first uses football as the name and has 11 players, the second uses quidditch as the name and has 7 players.

3.2 Create a class called Cereal that has three instance variables: name, brand, and fiber. When an instance of Cereal is printed, the user should see the following: “[name] cereal is produced by [brand] and has [fiber] grams of fiber in every serving!” To the variable name c1, assign an instance of Cereal whose name is Corn Flakes, brand is Kellogg’s, and fiber is 2. To the variable name c2, assign an instance of Cereal whose name is Honey Nut Cheerios, brand is General Mills, and fiber is 3. Practice printing both!

1. This problem will modify your previously defined class, bank. Add two more instance variables, deposits and withdrawals.Add two more methods, add_deposit and less_withdrawals. The add_deposit method should add the deposit amount to amt and the less_withdrawals method should subtract the withdrawal amount from amt. Create two instances of the class, the first assigned to bob and the second to sally. The first uses “Bob” as the name, 100 as the start_amt, and 50 as the deposit amount. The second uses “Sally” as the name, 200 as the start amount, 0 as the deposit, and 125 as the withdrawal amount. For bob, call add_deposit enough times so that the start_amt is 200 dollars and save to the variable bob_amt. For sally, call less_withdrawal enough times so that the start_amt is 75 dollars and save to the variable sally_amt.

4.1 Create a class called Sports that contains 3 instance variables, name, number_of_players, and recruit_max. Add the instance method that allows you to customize the message returned when you print the instance so that it says “The name of this sport is [name goes here] and [number_of_players goes here] people create one team!” There should also be another class method called recruiting, which will add the recruit_max to the current value for number_of_players.Create two instances of the class, one assigned to the variable football_info and one called quidditch_info. The first uses football as the name and has 11 players and a max number of 3 recruits, the second uses quidditch as the name and has 7 players and a max number of 2 recruits. Call recruiting enough times so that football_info has 17 players and that quidditch_info has 9 players.

4.2 Create a class called Cereal that has three instance variables: name, brand, and fiber. When an instance of Cereal is printed, the user should see the following: “[name] cereal is produced by [brand] and has [fiber] grams of fiber in every serving!” Create an instance method called add_fiber that increases an instance’s fiber count by 1 when it is called. To the variable name c1, assign an instance of Cereal whose name is Corn Flakes, brand is Kellogg’s, and fiber is 2. Call the add_fiber method until c1 has a fiber count of 5.

1. Challenge The class, Olympics, is given and has two instance variables, country and medal, referencing a country and its corresponding medal count in the Rio Olympics. The list, L, gives some countries and their medal counts. Create a list of instances from the given list and assign it to the variable instances. Then, sort the instances based on medal count and then alphabetically by country name. The sorted medal count list should be assigned to the variable sort_medal and be a list of tuples displaying both the country name and medal count from highest medal count to lowest. The list sorted alphabetically should only display the country name and be assigned to the variable sort_alpha.

5.1 Challenge: Provided is a class called called Music that has 3 instance variables, title, genre, and price. Provided is a list of tuples. Assign them to a new list and use the tuples to as parameters for instances of Music. Then, assign to the variable music_genre a sorted list of the instances’ titles by genre a-z. Assign to the variable music_price a sorted list of the instances’ titles by price, low to high. Hint: sorted won’t return the str method, so you will need to specify the title some way.

5.2 Challenge: Below, we have provided a list of tuples. Use these to create a list of instances of the House class. Each instance should have three instance variables: color, rooms, and price. When an instance is printed, the user should see: “This is a [color] house with [rooms] rooms that costs [price] dollars.” Save the list of instances as the variable houses. Then, sort the list based on price, highest to lowest, and save this list as houses_by_price. Finally, sort the list based on number of rooms, highest to lowest, and save this last as houses_by_rooms.

Next Section - Introduction: Class Inheritance