Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 3

10 bytes removed, 11:27, 10 August 2017
PART 1 - Providing Functions With Arguments
'''Passing up Multiple Arguments and Using Logic Statements'''
:You will now create a more complex function that will not only pass-up arguments, but also include '''logic''' to control the direction (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:'''
</source>
:#Define the following function in your ipython shell:<source>
def check_temperaturedescribe_temperature(temp):
if temp > 30:
return 'hot'
</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) to confirm the results:<source>
check_temperaturedescribe_temperature(50)
'hot'
check_temperaturedescribe_temperature(20)
'perfect'
check_temperaturedescribe_temperature(-50)
'cold'
check_temperaturedescribe_temperature(25)
'ok'
check_temperaturedescribe_temperature(10)
'ok'
</source>
:'''Perform the Following Instructions:'''
:#Create the '''~/ops435/lab3/lab3c.py''' script. The purpose of the script is to make 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 function. If the operate function does NOT understand the operator given, it should return a an error message.
:#Use this template to get started:<source>
#!/usr/bin/env python3
print(operate(10, 5, 'subtract'))
print(operate(10, 5, 'multiply'))
print(operate(10, 5, 'divide'))
</source>
:::*The operate() function should use '''logic''' 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 return pass a string value. You should be able to use double quotes within the message as shown in the sample runs.:::*The script should contain show the exact output as the sample imports
:::*The script should contain no errors
:::*As an extra exercise, try to write your function with only one return statement
:::'''Sample Run 1:'''<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ls CheckLab3.py || wget matrix.senecachttps://raw.ongithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab3.py
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 to the next step.
 
::Make notes for all of your observations in your lab log book, and proceed to Part 2.
<br><br>