OPS445 Lab 2

From Littlesvr Wiki
Revision as of 10:18, 10 January 2025 by Andrew (talk | contribs) (→‎User Input)
Jump to navigation Jump to search

!!! THIS LAB IS NOT READY YET !!!

User Input

Asing the program user for input on the command-line is a rather unusual thing to do these days, but it will help you get used to several basic concepts.

  • To begin, let's start out with a very basic script. This script will use objects that will display specific values to your terminal. Create the file lab2a.py in the ~/ops435/lab2 directory, containing the following content:
    #!/usr/bin/env python3
    
    name = 'John'
    age = 20
    print('Hi ' + name + ', you are ' + str(age) + ' years old.')
    
  • Try running this script and study the output:
    ./lab2a.py
    

This Python script is not very useful: it always displays the same output.

You can use the input() function to obtain information from the user and store it into a variable. The argument you pass to the input() function will be the prompt the user will see.

  • 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):
    colour = input("Type in a colour and press enter: ")
    
  • When prompted, type the text: red and press ENTER. Did anything display? Why not?
  • Add another line to your script:
    print(colour)
    

Thr print() function will display the contents of the colour variable (it's a variable and not the string "colour" because it's in quotes). The contents of the colour variable are set to whatever the input() function returned, which is whatever the user typed in before pressing Enter.

  • Now replace that line with this:
print("The colour I typed in is: " + colour)

This appears trivial, but again: take the time to understand what's happening here:

  1. Your program calls the Python input() function, passing it a string.
  2. The input function prints the string it received on the terminal, and waits for the user to type something in and press Enter.
  3. After the user does that: the input function returns the string which the user typed in back to your program.
  4. Your program takes the return from the input function and stores it in the colour variable.
  5. Your program then concatenates the two strings "The colour I typed in is: " and the value in the colour variable.
  6. Your program calls the print() function, and passes to it that concatenated string as a parameter.

When you start understanding software in this manner: you're starting to think as a programmer.

  • Make a copy of lab2a.py and call it lab2b.py
  • Modify lab2b.py so that it prompts the user for both the user's name and age. Your program should:
  1. Have a Shebang line
  2. Use a variable called name
  3. Use a variable called age
  4. Prompt the user for "Name: "
  5. Prompt the user for "Age: "
  6. Store the values in the correctly spelled variables (case sensitivity counts)
  7. Print the exact output as shown. Think through what type of variable age is.

Sample run 1:

./lab2b.py
Name: Jon
Age: 20
Hi Jon, you are 20 years old.

Sample run 2:

./lab2b.py
Name: Jen
Age: 25
Hi Jen, you are 25 years old.
  • Before proceeding, make certain that you identify any and all errors in lab2b.py. When the check script tells you everything is OK, you may procede to the next step.