Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON:(Please help me solve this assignment quickly.Thanks!!!) First Script - Variable-Length Keyword Arguments - kwargs This exercise develops and extends concepts introduced in the second

PYTHON:(Please help me solve this assignment quickly.Thanks!!!)

First Script - Variable-Length Keyword Arguments - kwargs

This exercise develops and extends concepts introduced in the second script of the take-home lab F.

Here, we will be passing a variable number of keyword arguments to a function. You might recognize that the terminology "variable number of keyword arguments" is just another way of describing a dictionary. In fact, this is what we will be doing - passing a dictionary to a function and using certain of the key(word) values - see functions (scroll down to the section called Arbitrary Number of Keyword Parameters).

For this exercise, imagine that there is a complex piece of equipment, perhaps a car or a spacecraft, and that all of its various subsystems periodically send out status messages to be read and evaluated by an overseer system. Each message contains just a small amount of data - perhaps only one or two keywords out of the hundreds of things that overseer system must monitor. However, we will restrict ourselves to only three things: temperature, CO2level, and miscError.

Write an overseerSystem function that has a kwargs argument.

Within the function, see if the keyword temperature exists in kwargs. If it does, and the temperature is greater than 500, print a warning with the temperature. Also see if the keyword CO2level exists in kwargs. If it does, and the CO2level is greater than .15, print a warning with the CO2level. Lastly, see if the keyword miscError exists in kwargs. If it does, print a warning with the miscError number.

Test from main by creating five messages and calling the overseerSystem function with each message.

Message1 is temperature:550 Message2 is temperature:475 Message3 is temperature:450, miscError:404 Message4 is CO2level:.18 Message5 is CO2level:.2, miscError:418

Sample Execution Results:

Warning: temperature is 550 Misc error #404 Warning: CO2level is 0.18 Warning: CO2level is 0.2 Misc error #418 

Second Script - Operator Overloading

We will be building a BritCoins class that allows you to work with British money as it was denominated up to 1971 - see: Old English Money. While there were a variety of types of coins, we will only be concerned with pounds, shillings, and pennies. A shilling was worth 12 pennies, and a pound was worth 20 shillings. Therefore, a pound was worth 240 pennies.

The class will allow you to initialize a BritCoins object with any given amount of pounds, shillings and pennies, add and subtract BritCoins objects, and to print a string that represents the value of the coins used to initialize a BritCoins object.

Certain aspects of the BritCoins class will parallel the Length class example shown here: magic methods (scroll down to the Example class: Length section). However, there will be some significant differences.

The idea of the BritCoins class is that you will pass the class a dictionary of coinTypes (keys) and the quantities of each coinType (values). In other words, we will be using kwargs as we did in the third exercise of the Lab H Takehome. The class will then calculate how many pennies these coins represent and save this totalPennies value. The totalPennies value will later be used for addition and subtraction as well as for generating the coin value string.

Building the class:

Create a BritCoins class with the following methods: __init__, __add__ , __sub__ , __str__. There will also be a private class dictionary called __coinValues as follows (this is similar to the __metric variable in the Length example).

__coinValues = {"pound":240, "shilling":12, "penny":1} #value of each type of coin in pennies

The __init__ method should have the parameters self, and **kwargs. Start by initializing a self.totalPennies attribute to zero. Then iterate through the kwargs. For each coinType in kwargs, add the value in pennies of that type and quantity of coin to a self.totalPennies. Determine this value by multiplying the number of coins (get this from kwargs) by the coin value (get this from __coinValues - do not hardcode the values!). As an example, if init receives 4 pound, 3 shilling and 2 penny, totalPennies should be 998 (4*240+3*12+2*1).

The __add__ method should have the parameters self and other. Calculate a local total by adding self.totalPennies to other.totalPennies. Then return this value. However, there are two crucial considerations here. The first is that when we add two BritCoins objects, the result of this addition should also be a BritCoins object. Therefore, what we want to return isn't the total, but is instead a new BritCoins object that has been initialized with the appropriate coinage. So, we need to create and return an instance of the class. We can do this within the class itself! The second consideration is that when we create this instance, we need to provide an appropriate argument for BritCoins. We can't just pass the total (of say, 998) - we need to pass a dictionary like {"penny":998}. Further, we need to use appropriate notation when making this BritCoins call (as you did when testing the overseerSystem function in the Lab H Takehome).

The __sub__ method should have the parameters self and other. It is similar to the add method except that you subtract other from self instead of adding it.

The __str__ method has the parameter self and should return a string that represents the value of the BritCoins object. The string should be formatted as shown below, showing pounds, shillings and pennies. From self.totalPennies, you will need to calculate how many of each coin type there are using floor division or some other technique. Note that this string representation won't necessarily be the coins used to initialize the BritCoins object - it will be the value of the BritCoins object. For example, if the BritCoins object was initialized with 25 shillings, the return should be 1 pounds 5 shillings 0 pennies.

For this script, the test code should be after your class code don't worry about calling from main.

Test the BritCoins class with the following data:

c1 = 4 pound, 24 shilling, 3 penny c2 = 3 pound, 4 shilling, 5 penny c3 = c1 + c2 c4 = c1 - c2

Then print c1, c2, c3 and c4.

Sample Execution Results:

5 pounds 4 shillings 3 pennies 3 pounds 4 shillings 5 pennies 8 pounds 8 shillings 8 pennies 1 pounds 19 shillings 10 pennies 

Add the following at the end of the scripts to show your results:

''' Execution results: paste execution results here '''

Print and staple together the scripts, including the execution results, and turn it in.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions