Changes

Jump to: navigation, search

OPS435 Python Lab 3

164 bytes added, 13:19, 14 August 2017
PART 1 - Providing Functions With Arguments
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 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 a function called '''square()''':<sourcelang="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 your '''square()''' function:<sourcelang="python">
square(5)
square(10)
square('2')
</source>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 the function '''sum_numbers()''':<sourcelang="python">
def sum_numbers(number1, number2):
return int(number1) + int(number2)
</source>
:#Issue the following from the ipython shell to see what happens:<sourcelang="python">
sum_numbers(5, 10)
sum_numbers(50, 100)
</source>
:#Issue the following to issue a function within another function:<sourcelang="python">
square(sum_numbers(5, 5))
</source>'''NOTE:''' Running functions with multiple arguments is the same. When you put a function as a argument of another function, the '''inner-most function will run first''', and the return the value '''10''' from the 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>
:::'''Python Script Template'''
:::<sourcelang="python">#!/usr/bin/env python3
def sum_numbers(number1, number2):
::*The script should contain no errors
::'''Sample Run:'''<sourcelang="python">
run lab3b.py
15
50
</source>
::'''Sample Run Using import:'''<sourcelang="python">
import lab3b
</source>
::2. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<sourcelang="bash">
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ipython3
</source>
:#Define the following function in your ipython shell:<sourcelang="python">
def describe_temperature(temp):
if temp > 30:
:'''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 an error message.
:#Use this template to get started:<sourcelang="python">
#!/usr/bin/env python3
Error: function operator can be "add", "subtract", or "multiply"
</source>
:::3. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<sourcelang="bash">
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory

Navigation menu