Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

72 bytes removed, 09:30, 22 January 2018
PART 1 - User Input
== PART 1 - User Input ==
:In this section, you will learn how to prompt (ask) the user running the program for input or data. Although you will not be immediately be using the information that the user provided, you will use that information later in this lab to change how a Python script works under in different situations.
'''Storing User Input In Variables'''
:'''Perform the following steps:'''
:#Launch your Centos VM, open your code editor, and a a shell terminal (as a regular user) and start a new ipython3 session:<source>ipython3</source>for executing your code.:#To begin, let's start out with a very basic script. This script will use variables that will display specific information to your terminal. Move to Create the file '''lab2a.py''' in your '''~/ops435/lab2''' directory, create the file '''lab2a.py''' with a text editor containing the following content:<source lang="python">
#!/usr/bin/env python3
</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 typical 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 Replace the print() call in your '''ipython3''' shell and type lab2a.py with the following code (do you can just comment-out the print() call using a '''NOT#''' edit at the lab2a.py file, just issue from beginning of the shellline): <source lang="python">
colour = input("Type in a colour and press enter: ")
</source>
:#When prompted, type the text: '''red''' and press ENTER. Did anything display? Why not?
:#Issue the following in the ipython3 shellAdd another line to your script:<source lang="python">print(colour)
</source>What was displayed?
:#Issue the following in the ipython3 shellAnd replace that line wiht this:<source lang="python">print('The colour I typed in is: ' + colour)
</source>Note what was displayed.
:#Exit the ipython3 shell, Now download the checking script and check your work. Enter Run the following commands from the bash shell.:<source lang="bash">
cd ~/ops435/lab2/
pwd #confirm that you are in the right directory
Hi Jen, you are 25 years old.
</source>
:::3. Download the checking script and check your work. Enter Run the following commands from the bash shell.<source lang="bash">
cd ~/ops435/lab2/
pwd #confirm that you are in the right directory