Download (right-click, save target as ...) this page as a jupyterlab notebook from: Lab4


Laboratory 4: Input and Output

LAST NAME, FIRST NAME

R00000000

ENGR 1330 Laboratory 4 - In-Lab

Input

Useful programs take input and generate output

So here we practice capturing input from the keyboard (most likely)

First lets explore the input() function

Exercise

Modify Case 3 to make the fries numeric (integer or float), then run the script to validate the modification

About the prompt

It is a good idea to tell the user what to input. You can do this by putting the hint in quotes inside the input parentheses. The hint will come in the next line and will wait for the user input. You can then type the input and when you hit the ENTER key, it will capture the input.

In this example, "tell me a beautiful number" is the hint. This gets printed in the next line when asking for the input. If you type 6, this will be assigned to the variable beautiful_number which we can print later.

The prompt can be any string variable

Or the prompt can be drawn from a list of prompts like in the Cyberdyne 101 shown above in its remote desktop mode (from Terminator (1985) approximately at timestamp 1:14:07)

Below the script might look something like (remember the terminator is responding to an input from the apartment super, and is responding). The point of the example is that the prompt in the input() function can be drawn from a list rather than retyping each use.

Exercise

Run the script below a few times. It should output a randomly selected integer from 0 to 6. Then copy the Cyberdyne prompt list above to the location shown, replace the last two placeholder strings with the responses from the movie (in the image above). Then run the script 6 times; how many times does the terminator respond with your replacements for the placeholders?

Example. Suggestive Sell!

Suppose we are building an automated food service program. If you recall from real life, that when you complete your order the cashier always asks "would you like fries with that?"

Well lets script the interaction, and get either a yes or no answer from the customer.

In these examples we don't operate on the input much, however lets conclude the beautiful number example, and do something with it

Exercise/Discussion

How could you approximate the square root using just the tool (script) above?

Try it for:

About Output

Later on we will learn about printing to a file (here we use the word print as a surrogate for output, not necessarily actually printing something); the remainder of this lab explores some of the necessary ideas involved in output

We have already examined and played with "Hello World" as shown below - but I added some interaction.

We can also output values from a list such as

Or the list can be numeric

Print blank lines by using the newline symbol, which is \n.

Example

Let's build a script thats asks the users name, then says hello. Skips 8 lines and identify ourself as Cyberdyne Terminator Model 101.

The work flow is nothing more than

Using the bullet list as our structure generator we can start with

Now copy-paste our prompt example

Next lets print 8 linefeeds

Now we add our response.

Notice the step-by-step build of the script; obviously you dont need to copy and paste so many cells, but here we want to be overly anal as we learn the JupyterLab environment.

Formatting the output

In the lecture we learned about formatting using the % operator and the format() method. They are roughly the same, I think the % approach is a little easier to read when maintaining scripts, but you may decide otherwise.

As an example we can combine the Terminator example and beautiful number example.

About ending the line

By default, print function in Python ends with a newline. This function comes with a parameter called 'end.' The default value of this parameter is \n , i.e., the new line character. You can end a print statement with any character or string using this parameter, but you have to specify in the print statement.

Exercise

Explore the %.3f format code above, what happens if you change the numeric part of the format code?