- Parent Category: Programming Assignments' Solutions
We Helped With This Python Programming Assignment: Have A Similar One?
|More Info||Python Assignment|
Short Assignment Requirements
This lab introduces modules, which allow the usage of functions from different files. You've already seen the use of module import, as random, math, and scipy. Imagine a large program that calls several different functions you've defined within it. Now, you'd like to make a second program – and many of the functions in the first are very helpful in the second. You could rewrite them again in the second program, or simply import them, which allows access to them without having to rewrite anything.
Here we will import functions you've written to illustrate how importing works. As long as both files are in the same directory, it can be imported. Python also has a special directory within its installation folder that stores packages that are installed from the web or elsewhere (like SciPy). When using the import keyword, this directory as well as the directory you're working in are searched for the module.
• Exercise 1: Creating a Function
• Exercise 2: Importing a Function
Exercise 1: Create a Function to Calculate the Cost of Gas
Overview: In the drive to work program, we have been asking the user for the cost of gasoline, but haven’t put that variable to use. We will make a function now that takes in cost, distance, the usage rate, and returns a total.
1. Open function_test.py from last lab.
• Reopen the function_test.py program. If you haven’t closed it, it should exist as a tab in Spyder.
• If you have lost this file, just create a new blank file.
• Clear out the main portion of this file, leaving only the function definitions.
2. Define a function calc_price(cost, distance,rate):
• Recall, rate is in distance per unit of fuel.
• You will need to calculate the units of fuel for a given distance, multiply that by cost, and return the value.
• Create a DocString for this function ("""underneath the def line, with triple quotes""") that describes what it does.
• Test your function within this program to verify it calculates properly, e.g.:
• calc_price(3,100,200) should return 6.0
3. Import your new function to another program.
• Open any of the programs we've created before, or open a new file.
• Because Spyder saves all of these files in the same directory, they all have access to each other using import.
• Type import function_test near the top of this file. You now have access to those functions!
• Now, anywhere within the main portion of this program, type:
• Anaconda will list the available functions. You should have do_exp (if you used your last function_test.py) and calc_price. Also you have access to random, since it was imported for function_test.py.
• Choosing calc_price, clicking the word so the cursor is within it, and hitting Ctrl+I shows the DocString you wrote earlier.
• Try calling one of those functions and running the program. Remember to use print lines where necessary to verify results.
Exercise 2: Import the Function into Branching.py
Overview: We will now implement our function into the Drive to Work program stored in branching.py.
1. Import your function into branching.py
• Open branching.py and import function_test. Remember imports should happen at the top of the program underneath.
• Verify it is usable by typing: function_test. Spyder should show you the available functions.
• Call your calc_price function above the while loop for review, using the appropriate values taken from the user.
• Add a print line within the review section that reports the total cost of the trip, based on the value returned by function_test.calc_price().
• Remember to use the appropriate format specifiers for currency!
• You can also add to the #reporting strings section in case the user doesn't want to review the values, but would still like to know how much they expect to spend on this trip.
2. Test your program
• Run the program and verify it works as intended.
• If you have issues, make sure indentation is correct (Remember, we have multiple loops!)