Changes

Jump to: navigation, search

OPS435 Python Lab 3

54 bytes added, 12:17, 14 August 2017
PART 2 - Creating a Python Script with Functions and Importing Functions
:::*Add another '''empty line''' following by the '''return_number_value()''' function '''definition''' that you previously entered in the shell.
:::*Add a '''couple of empty lines''', following by a comment stating: '''# Main Program'''
:::*Add another '''couple of empty lines''', followed by the statements displayed below:<sourcelang="python">
if __name__ == '__main__':
print('python code')
:'''Perform the following Instructions:'''
:# Let's see what happens if we forget to import functions from your lab3a.py script prior to calling a function. Issue the following:<sourcelang="python">
text = lab3a.return_text_value()
</source>You should notice an error indicating '''"name 'lab3a' is not defined"'''. This error occurs since you failed to instruct the ipython shell to '''import''' or "load existing defined functions from your lab3a.py script" to '''internal memory'''.<br><br>
:# Issue the following within the ipython shell:<sourcelang="python">
import lab3a
text = lab3a.return_text_value()
lab3a.return_number_value()
</source> You should notice that all of the function calls should now work. '''NOTE:''' since you are in the ipython shell, the import command only will work as long as you are '''logged into that shell'''. You will need to use other methods (contained in other Python Scripts (or future ipython shell sessions) to make these function imports '''persistent''', but you will learn about that at a later time.<br><br>
:# 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