CS 171 Fall 08 

Prelab 2: The Basics

Due: Wednesday, September 9th at 11:59pm

 

In this prelab you'll get further acquainted with basic input and create one program to read in strings from the user and print a greeting.

Part 1 - Input and Greeting

In Python, we have two built-in functions for retrieving input from the user.

input(): This function retrieves a sequence of keystrokes from the user and evaluates what has been typed. The user is really typing an expression, which can consist of constant literals, operators, and even variables and function invocations. Whatever is typed is evaluated by the interpreter to compute the value of the expression (and its associated type), and this is the resultant value of the input() function invocation. This function is typically used when we wish to retrieve an integer or real-valued number from the user.

raw_input(): This function retrieves a sequence of keystrokes from the user, but does not evaluate what has been typed. The sequence of characters remains a string, and it is this string that is the resultant value of the raw_input() function invocation. This function is typically used when we wish to retrieve a string (which may or may not include spaces) from the user.

We first are going to practice using input() and raw_input() by using the Python Shell interpreter. If you bring up the Wing IDE and click the "Options" drop-down in the upper right corner, you will see a "Save a Copy" menu item. Don't click on this now, but this will enable us to get a text file with the set of interactions from the Python Shell window, so that you can show me that you completed this practice part.

For each of the following, you will type in the given input or raw_input invocation/assignment statement, and then the indicated sequence of characters in bold. We use assignment statements so that we can associate the variable testvar with the function invocation resultant value and can see both the value and the type. So after each input statement, I want you to predict the value and the type of testvar, and then to type in

>>> testvar
to see the value, and

>>> type(testvar)
to see its type.

In some cases, you may get an error. Try to predict this error and/or explain why it occurred.

  1. testvar = input("Enter an integer number: ")
    then enter 5
  2. testvar = input("Enter an real-valued number: ")
    then enter 5.723E2
  3. testvar = input("Enter a string without spaces: ")
    then enter Bressoud
  4. testvar = input("Enter a string without spaces: ")
    then enter "Bressoud"
  5. testvar = raw_input("Enter an integer number: ")
    then enter 5
  6. testvar = raw_input("Enter an real-valued number: ")
    then enter 5.723E2
  7. testvar = raw_input("Enter a string without spaces: ")
    then enter Bressoud
  8. testvar = raw_input("Enter a string without spaces: ")
    then enter "Bressoud"

Save your Python Shell interaction by using the Options meny and selecting "Save a Copy", putting it in a file named 'inputRun.txt' and put it in your Prelab02 folder for submission.

Create a program called Greetings.py which asks the user to enter their name, and then enter their nickname, and then prints "Welcome, <their name>, (or should I say <nickname>)!"

Part 2 - Uploading your lab

In your assignment dropbox create a folder called Prelab02.  Into this folder place: