Changes

Jump to: navigation, search

OPS435 Python Lab 3

153 bytes added, 03:25, 30 May 2017
no edit summary
== PART 1 - Using Functions ==
'''Functions and Strings'''
:'''Perform the following stepsFollowing Steps:'''
:#To start, open ipython and try experimenting with functions.<source>
ipython3
</source>
:::*The script should have a '''Shebang line''':::*The script should have a function sum_numbers(number1, number2):::*The script should have a function subtract_numbers(number1, number2):::*The script should have a function multiply_numbers(number1, number2):::*All functions should accept two arguments:::*All functions should return a integer:::*The script should contain no errors
::#Sample Run 1:<source>
The next function we make in this section if going to be more advanced and contain logic inside it. First try the following to practice logic in functions.
:'''Perform the Following Steps:''':#Start the ipython3 shell<source>
ipython3
</source>
 :#Now try making some functions that uses if statements, '''BUT BEWARE''', making an if statement inside a function means that you are indented two times to get to the if statement.<source>
def check_temperature(temp):
if temp > 30:
</source>
 :#Remember to note the extra indentation on the code under the if statements. 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.  <source>
check_temperature(50)
'hot'
</source>
'''lab3cPractice Multiple Levels of Indentation''':'''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 error message. :#Use this template to get started:<source>
#!/usr/bin/env python3
print(operate(10, 5, 'multiply'))
print(operate(10, 5, 'divide'))
 
</source>
 :::*The script should have a '''Shebang line''':::*The script should have a function operate(num1, num2, operator):::*The script should use if statements inside the operate function:::*The operate function should accept three arguments:::*The operate function should return the answer:::*The operate function should return a error message if the operation is unknown:::*The script should contain show the exact output as the sample imports:::*The script should contain no errors  ::#Sample Run 1:<source>
run lab3c.py
15
Error: function operator can be "add", "subtract", or "multiply"
</source>
 ::#Sample Import 1:<source>
import lab3c
operate(100, 5, 'power')
Error: function operator can be "add", "subtract", or "multiply"
 
</source>
  :#Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3c
</source>
:#Before proceeding, make certain that you identify any and all errors in lab3c.py. When the check script tells you everything is ok before proceeding to the next step.
198
edits

Navigation menu