1,760
edits
Changes
→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 data that the user provided, you will use that information data later in this lab to change how a Python script works in different situations.
'''Storing User Input In Variablesdata to Python objects'''
:'''Perform the following steps:'''
:#Launch your Centos VM, open your code editor, and a a shell terminal (as a regular user) for executing your code.
:#To begin, let's start out with a very basic script. This script will use variables objects that will display specific information values to your terminal. Create the file '''lab2a.py''' in your '''~/ops435/lab2''' directory, containing the following content:<source lang="python">
#!/usr/bin/env python3
:#Try running this script and study the output:<source>
./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 an object (also called a variable). It is typical to place a question (or hint) as a argument in the input() function: this will aid the user in typing in the correct informationdata.<br><br>
:#Replace the print() call in your lab2a.py with the following (you can just comment-out the print() call using a '''#''' at the beginning of the line): <source lang="python">
colour = input("Type in a colour and press enter: ")
</source>Note what was displayed.
''' Practice Storing User Input data '''
:Now it's time to create a new script to prompt the user to enter data and display that data on their terminal. Refer to variable object name and prompt text information when creating your Python script. Refer to Sample Runs displayed below for exact prompt and output requirements.
:'''Perform the following Instructions:'''
:::*The script should have a '''Shebang line'''
:::*The script should use a variable an object called '''name''':::*The script should use a variable an object called '''age'''
:::*The script should prompt the user for "Name: "
:::*The script should prompt the user for "Age: "
:::*The script should store the values in the correctly spelled variables objects (case sensitivity counts)
:::*The script should print the EXACT OUTPUT as shown (case sensitivity counts)