Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 3

183 bytes added, 00:38, 28 January 2018
PART 1 - Providing Functions With Arguments
:'''Perform the Following Steps:'''
:#Start the ipython3 shellCreate a new Python file for testing.:<source>ipython3</source>#When passing arguments to functions, you put data such as '''strings''', '''numbers''', or '''variable names''' within brackets immediately following the function name.<br><br>'''NOTE:''' If a function accepts arguments, then those rguments arguments must be declared (using variable names) when the function is declared. Those declared variable names are then used within the function for processing. Also, when you call a function with arguments, the number of arguments passed up to the function must match the number of arguments that were specified in the function declaration.<br><br>:#Issue the following from the ipython shell to declare Define a function called '''square()''':<source lang="python">
def square(number):
return number ** 2
</source>'''FYI:'''You may have learned that you multiple a number by itself in order to "square" the number. In python, the '''**''' operator will raise the operand on the left to the power of the operand on the right.<br><br>When calling functions with multiple arguments, the arguments are separated by '''commas'''. See what happens if you provide strings, strings without using quotes, or numbers with decimals in the following examples.
:#Issue the following to test Test your '''square()''' function:<source lang="python">
square(5)
square(10)
square(square(2))
square('2')
</source>Notice that nothing is printed, you need to print the values the functions return to see what they are.:#The last function call should produce an '''error message'''. This is caused by sending a '''string''' instead of a number that is processed by the function. We could use the int() function to convert any value passed in as a string by mistake to an integer number.<br><br>:#Issue the following to declare Declare the function '''sum_numbers()''':<source lang="python">
def sum_numbers(number1, number2):
return int(number1) + int(number2)
</source>
:#Issue the following from the ipython shell Call that function to see what happens:<source lang="python">
sum_numbers(5, 10)
sum_numbers(50, 100)
</source>
:#Issue the following to issue You can also do what looks like calling a function within another function, but it's actually just calling sum_numbers() first, then calling square() with the return from sum_numbers as an argument:<source lang="python">
square(sum_numbers(5, 5))
</source>'''NOTE:''' Running functions with multiple arguments is the same. When you put call a function as a an argument of another function, the '''inner-most function will run first''', and the return the value '''10''' from the that will be used as the argument for the '''outer function'''. In the example below, '''sum_numbers(5, 5)''' will return '''10''', thus providing that result to be square with that value '''square(10)'''.<br><br>
'''Practice Creating a Function that Accepts Arguments and Returns a Value'''
::'''Sample Run:'''<source lang="python">
run ./lab3b.py
15
5
50
</source>
::'''Sample Run Using importOther examples:'''<source lang="python">
import lab3b
lab3b.sum_numbers(10, 5)
# Will return 15
lab3b.sum_numbers(25, 25)
# Will return 50
lab3b.subtract_numbers(10, 5)
# Will return 5
lab3b.subtract_numbers(5, 10)
# Will return -5
lab3b.multiply_numbers(10, 5)
# Will return 50
lab3b.multiply_numbers(10, 2)
# Will return 20
</source>
::2. Exit the ipython3 shell, download Download the checking script and check your work. Enter the following commands from the bash shell.<source lang="bash">
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
</source>
::3. Before proceeding, make certain that you identify any and all errors in lab3b.py. When the checking script tells you everything is OK before proceeding - proceed to the next step.
'''Passing up Multiple Arguments and Using Logic Conditional Statements'''
:You will now create a more complex function that will not only pass-up arguments, but also include logic to control the flow of the function, and affect how your Python script will be run. You will create a function that uses an '''if/elif/else''' logic statement.
:'''Perform the Following Steps:'''
:#Start the ipython3 shell<source>ipython3</source>:#Define Use a temporary Python file to define the following function in your ipython shell:<source lang="python">
def describe_temperature(temp):
if temp > 30:
return 'ok'
</source>The final '''return "ok"''' will only take place if a previous return has not taken place before it. Once return has been used in a function, the function immediately exits and returns the value.
:#Issue the following functions (with arguments) Call describe_temperature like this to confirm the results:<source>
describe_temperature(50)
# Will return 'hot'
describe_temperature(20)
# Will return 'perfect'
describe_temperature(-50)
# Will return 'cold'
describe_temperature(25)
# Will return 'ok'
describe_temperature(10)
# Will return 'ok'
</source>
:'''Perform the Following Instructions:'''
:#Create the '''~/ops435/lab3/lab3c.py''' script. The purpose of the script is to make have a single function that can perform addition, subtraction, or multiplication on a pair of numbers. But the function will allow us to choose exatly what operation we are performing on it when we call the functionit. If the operate function does NOT understand the operator given, it should return an error message.
:#Use this template to get started:<source lang="python">
#!/usr/bin/env python3
print(operate(10, 5, 'divide'))
</source>
:::*The operate() function should use '''logicconditional''' statements<br> &nbsp; '''FYI:''' Remember that you MUST consistently '''indent ALL code''' for within each logic section (or test): otherwise, it may not allow the logic statement to work correctly. :::*The operate() function should accept '''three arguments'''.:::*The operate() function should '''return''' the result.
:::*The operate() function should '''return''' an error message if the operation is unknown<br> &nbsp; '''FYI:''' Use single quotes or double-quotes to pass a string value.
:::*The script should contain show the exact output as the sample.:::*The script should contain no errors.:::*As an extra exercise, try to write your function with only one return statement.
:::'''Sample Run 1:'''<source>
run ./lab3c.py
15
5
</source>
:::'''Sample Run 2 (with using importfrom another Python file):'''<source>
import lab3c
lab3c.operate(10, 20, 'add')
# Will output 30
lab3c.operate(2, 3, 'add')
# Will output 5
lab3c.operate(100, 5, 'subtract')
# Will output 95
lab3c.operate(10, 20, 'subtract')
# Will output -10
lab3c.operate(5, 5, 'multiply')
# Will output 25
lab3c.operate(10, 100, 'multiply')
# Will output 1000
lab3c.operate(100, 5, 'divide')
# Will output Error: function operator can be "add", "subtract", or "multiply"
lab3c.operate(100, 5, 'power')
# Will output Error: function operator can be "add", "subtract", or "multiply"
</source>
:::3. Exit the ipython3 shell, download Download the checking script and check your work. Enter the following commands from the bash shell.<source lang="bash">
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3c
</source>
:::4. Before proceeding, make certain that you identify any and all errors in lab3c.py. When the checking script tells you everything is OK before proceeding - proceed to the next step.
<br><br>