Changes

Jump to: navigation, search

OPS435 Python3 Lab 3

648 bytes added, 16:25, 19 September 2019
INVESTIGATION 2: CREATING FUNCTIONS WITH ARGUMENTS AND RETURN VALUES
<br><br>
= INVESTIGATION 2: CREATING FUNCTIONS WITH ARGUMENTS AND RETURN VALUES =
== PART 1 - Providing Functions With Arguments ==
:'''Perform the Following Steps:'''
:#Create a new Python file for testing.
:#When passing arguments to functions, you put data such as '''strings''', '''numbers''', or '''variable other object names''' within brackets immediately following the function name.<br><br>'''NOTE:''' If a function accepts arguments, then those arguments must be declared (using variable argument 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>
:#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, which will give the same result as muliple the number by itself.<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.
:#Test your '''square()''' function:<source lang="python">
def square(number):
return number ** 2
square(5)
square(10)
square(square(2))
square('2')
</source>Notice that nothing is printedby the above script, you need to print the values the functions return to see what they are.Update your test script as shown below:<source lang="python">def square(number): return number ** 2print(square(5))print(square(10))print(square(12))print(square(square(2)))print(square('2'))
:#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>
:#Declare the function '''sum_numbers()''':<source lang="python">
:::<source lang="python">#!/usr/bin/env python3
'''Lab 3 Part 1 script - functions'''
# Author ID: [seneca_id]
def sum_numbers(number1, number2):
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ls CheckLab3.py || wget https://rawict.githubusercontentsenecacollege.comca/Seneca-CDOT~raymond.chan/ops435/masterlabs/LabCheckScripts/CheckLab3.py
python3 ./CheckLab3.py -f -v lab3b
</source>
'''Passing up Multiple Arguments and Using Conditional Statements'''
:You will now create a more complex function that will not only pass accept arguments, but also include logic to control the flow of the functionand what value it returns, and therefore affect how your Python script will be runresponse. You will create a function that uses an '''if/elif/else''' statement.
:'''Perform the Following Steps:'''
return 'perfect'
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 valueto the caller.
:#Call describe_temperature like this to confirm the results:<source>
print(describe_temperature(50))
</source>
'''Create a Python Script Function Receiving Multiple Arguments'''
:'''Perform the Following Instructions:'''
:#Create the '''~/ops435/lab3/lab3c.py''' script. The purpose of the script is to 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 exactly what operation we are performing want to perform on it the two numbers when we call it. If the operate function does NOT understand the operator given, it should return an error message (e.g. calling the function to 'divide' two numbers).
:#Use this template to get started:<source lang="python">
#!/usr/bin/env python3
'''Lab 3 Inv 2 function operate '''
# Author ID: [seneca_id]
def operate(number1, number2, operator):
:::*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 produce the exact output as the samplerun shown below.
:::*The script should contain no errors.
:::*As an extra exercise, try to write your function with only one return statement.
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ls CheckLab3.py || wget https://rawict.githubusercontentsenecacollege.comca/Seneca-CDOT~raymond.chan/ops435/masterlabs/LabCheckScripts/CheckLab3.py
python3 ./CheckLab3.py -f -v lab3c
</source>
<br><br>
== PART 2 - Running System Commands with Subprocess subprocess ==:The remainder of this investigation will allow you to run operating explore the behaviour of some built-in functions, all of them will take function argument(s), some will return returns, but some will not. The built-in functions we are going to explore are: os.system commands via your Python script(), os.popen(), subprocess.Popen(). Although there are different ways in which All of them can be used to issue operating system commands, you will learn two of them.
'''Perform the Following Steps:'''
1,760
edits

Navigation menu