Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

18 bytes removed, 13:23, 1 August 2017
no edit summary
</source>
:#Try running this script inside ipython3 and study the output:<source>
run ./lab2a.py
</source>This Python script is not very useful: it displays the same output regardless of the number of times that the Python script is run.<br>The '''input()''' function is used to obtain information from the user and store it into a variable. It is recommended to place a question (or hint) as a argument in the input() function: this will aid the user in typing in the correct information.<br><br>
:#Return to your '''ipython3''' shell and type the following code (do '''NOT''' edit the lab2a.py file, just issue from the shell): <source lang="python">
:::'''Sample run 1:'''<source>
run ./lab2b.py
Name: Jon
Age: 20
</source>
:::'''Sample run 2:'''<source>
run ./lab2b.py
Name: Jen
Age: 25
</source>
:#Run the script and examine the output by running the Python script without and with arguments:<source>
run ./showargs.pyrun ./showargs.py testing different argumentsrun ./showargs.py argument1 argument2 argument3
</source>
:::'''Sample run 1:'''<source>
run ./lab2c.py Jon 20
Hi Jon, you are 20 years old.
</source>
:::'''Sample run 2:'''<source>
run ./lab2c.py Jen 25
Hi Jen, you are 25 years old.
</source><br>Note that running '''Sample run 3''' (shown below) will result in an error message. This error happens if you run the script without any arguments. It is important to note that an error such as this can occur, so you can avoid this type of error when creating Python scripts (explained in the next section). Let's continue in this section for now...
:::'''Sample run 3:'''<source>
run ./lab2c.py
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)