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.)
print(exp1, exp2, ..., expN)
Prints the expressions
exp1
throughexpN
, each separated from the next by a space, in an output box.
str1 + str2
If
str1
andstr2
are strings: creates the string containing all the characters ofstr1
immediately followed by all the characters ofstr2
.
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.
the line containing
s_length = input( "Enter a length (in pixels): " )
-
No, this command assigned the string of characters that you typed into the input box to
s_length
. Look at the contents of the line number mentioned in the error box. the line containing
b_color = input( "Enter a color: ")
-
No, this command assigned the string of characters that you typed into the input box to
b_color
. Look at the contents of the line number mentioned in the error box. the line containing
b_diameter = s_length/b_number
-
Yes! The interpreter was not able to calculate
s_length/b_number
becauses_length
holds a string value — that is, a sequence of characters — and division is not defined for strings. the line containing
b_radius = b_diameter/2
-
No, the interpreter stopped executing the program when the error occurred in line 9. So it never even executed this line
Q-5: At what line did the interpreter find an error?
TypeError
-
Yes! The interpreter shows the kind of error at the very start of the error message.
NameError
-
No. The interpreter shows the kind of error at the very start of the error message.
DivisionError
-
No. The interpreter shows the kind of error at the very start of the error message.
Gross Error
-
No. The interpreter shows the kind of error at the very start of the error message.
Q-6: What is the name of the error that occurred?
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:
int
For representing whole numbers (integers)
Examples:
0 2020 -35
float
For representing decimals (floats)
Examples:
0.0 3.1416 -.75
str
For representing text (strings)
Examples:
'Coders rule!' "Practice makes perfect." """12/25/2020""" """Only tripled quoted strings can go for more than one line."""
bool
For representing either true or false
The values of type
bool
are:True False
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.
Under construction.
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.