Technovation logo

6.4. Learning to Code: User Input

How useful would computers be if you couldn’t interact with a program while it was running?

User input refers to information that a user provides when running a program in order to affect what the program does.

Q-1: For example, a computer game may ask you to input the number of players and a name for each. What do you think the game might use this information for?

Python provides the input command for user input. It must be called with a string argument, which consists of characters that are enclosed in matching quotations. The argument should be a message, or prompt, that you want the interpreter to print before collecting the input.

To execute an input command, the interpreter displays an input box in which it prints the prompt (argument). It then waits for the user to type something into the box and press either the enter key or the OK button, which signals that the user is done. The interpreter uses the string of characters typed by the user into the input box as the input value.

You will usually want to assign the input value to a variable so that you can use the value in later commands.

The program below shows the typical way to get and use input. Run it three or more times; each time, type a different name, phrase, or whatever you would like into the input box. Then press either the enter key or the OK button.

(Be sure to scroll your window so you can see the output box that the interpreter creates when it executes the print command.)

Let’s explore how you might use input in a Turtle Graphics program.

Read the program below and predict what it will draw. Then run it to verify that you understand how the program works. If you are uncertain, chat with a mentor about it. You should understand the program before proceeding any further.

This program will always draw a string of red beads. A more useful program might let the user decide what color to make the beads.

To do this, replace the string "red" in line 7 of ac-TG-input with

input( "Enter a color: ")

Now, when you run the program and the interpreter gets to line 7, it will bring up an input box containing the prompt and wait for you to type something into the box. Type the name of a different color (without any quotes) into the input box and then press enter. If you typed a known color name, it draws the string of beads in this color. How cool is that!

In addition to the bead color, you might like to let the user decide how long the string of beads should be and how many beads it should contain.

To let the user choose the length, try replacing the 300 in ac-TG-input with:

input( "Enter a length (in pixels): " )

Q-4: What happens when you run the program in ac-TG-input after making the suggested replacement?

See if you are can find answers to the following questions by reading what it says in the error box (the light red box) now displayed below the editor window.

To understand the problem we are bumping into, we need to talk about the types of values.

Every programming language provides different types of values. Python provides four primitive data types:

Mike’s rap about variables and types may help you remember them, and maybe even understand them a bit better. But keep in mind that Mike’s rap is about general languages, and that types in Python are a bit simpler. In particular, Python does not have a char type and it does not let you write the type at the beginning of an assignment statement.

Armed with this information about types, let’s look again at the program we are working on and the error it produces:

The program is copied below and the input commands are added. Run this program and type a whole number in the first input box and a color name in the second.

The error message tells you that the interpreter could not execute the instruction on line 9 because of a type error (TypeError). It also tells you that it cannot perform division (Div) on values of type str and int. Looking at line 9, you can see that your code says to divide the value of s_length by the value of b_number and assign the result to b_diameter. So the problem is that, at line 9, b_number contains a string, not a number!

Why is b_number a string? Because the input command always returns a string value! For example, suppose you run the program and type 300 into the first input box; then the value of b_number at line 9 will be the string "300", which is not the same as the number 300!

How can you fix this problem? Python provides an int function for exactly this kind of situation. If you call int with a string of numbers, it creates and returns the int value represented by that string. For example, int("300") returns 300.

In ac-TG-input-error, add the following assignment after line 5:

s_length = int(s_length)

Then run the program and check that it no longer produces an error, and that you can get it to draw longer and shorter strings of beads of different colors.

Finally, modify the program to also ask the user for the number of beads. Your final program should ask the user for the length to make the string, the number of beads to use, and the color. Then it should draw a string of beads of the given length and containing the given number of beads of the given color.

Chat with your mentor if you encounter any additional issues.

You have attempted of activities on this page