Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
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
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
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
(Be sure to scroll your window so you can see the output box that the
interpreter creates when it executes the
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
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
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:
For representing whole numbers (integers)
0 2020 -35
For representing decimals (floats)
0.0 3.1416 -.75
For representing text (strings)
'Coders rule!' "Practice makes perfect." """12/25/2020""" """Only tripled quoted strings can go for more than one line."""
For representing either true or false
The values of type
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
char type and it does not let you write the type
at the beginning of an assignment
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 (
It also tells you that it cannot perform
Div) on values of type
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
So the problem is that, at line 9,
b_number contains a string, not a number!
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
is not the same as the number
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
value represented by that string.
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.