Lecture 11 Interacting With The User
Lecture 11 Interacting With The User
The user enters the number 37, but when we ask Python for the value of age, it
returns ‘37', the string representation of the numerical value entered.
We know Python interpreted the input as a string because the number is now
enclosed in quotes. If all you want to do is print the input, this works well. But if
you try to use the input as a number, you’ll get an error:
Using int() to Accept Numerical Input
When you try to use the input to do a numerical comparison (1), Python produces
an error because it cant compare a string to an integer: the string ‘21’ that’s
assigned to age cant be compared to the numerical value 18
This can be resolved by using the int() function, which tells python to treat the
input as a numerical value. The int() function converts a string representation of a
number to a numerical representation as shown below:
Using int() to Accept Numerical Input
How do you use the int() function in an actual program? Consider a program that
determines whether people are tall enough to ride a roller coaster:
The program can compare height to 48 because height = int(height) converts the
input value to a numerical representation before the comparison is made. If the
number entered is greater than or equal to 48, we tell the user that they’re tall
enough:
Do it yourself section (Exercise 1)
Write a program that asks you for your full name and student number and then
prints the following
YOUR NAME IS *************** AND YOUR STUDENT NUMBER IS
*******:
Write a programme that asks the user how many people are in their dinner group.
If the answer is more than eight, print a message saying they’ll to wait for a table.
Otherwise, report that their table is ready
Introducing while Loops
The for loop takes a collection of items and executes a block of code once for
each item in the collection. In contrast, the while loop runs as long as, or while, a
certain condition is true.
Let us examine the following code
In the first line, we start counting from 1 by assigning current number the value 1.
The while loop is then set to keep running as long as the value of current number
is less than or equal to 5. The code inside the loop prints the value of current
number and then adds 1 to that value with current number += 1.
(The += operator is shorthand for current number = current number + 1.)
Introducing while Loops
Python repeats the loop as long as the condition current number <= 5 is true.
Because 1 is less than 5, Python prints 1 and then adds 1, making the current
number 2. Because 2 is less than 5, Python prints 2 and adds 1 again, making the
current number 3, and so on. Once the value of current number is greater than 5,
the loop stops running and the program ends:
The programs you use every day most likely contain while loops. For example, a
game needs a while loop to keep running as long as you want to keep playing,
and so it can stop running as soon as you ask it to quit. Programs wouldn’t be fun
to use if they stopped running before we told them to or kept running even after
we wanted to quit, so while loops are quite useful.
Letting the User Choose When to Quit
Lets examine the following code
At ➊, we define a prompt that tells the user their two options: entering a message
or entering the quit value (in this case, 'quit'). Then we set up a variable message
➋ to keep track of whatever value the user enters. We define message as an empty
string, "", so Python has something to check the first time it reaches the while
line. The first time the program runs and Python reaches the while statement, it
needs to compare the value of message to 'quit', but no user input has been
entered yet. If Python has nothing to compare, it won’t be able to continue
running the program.
Letting the User Choose When to Quit
To solve this problem, we make sure to give message an initial value. Although
it’s just an empty string, it will make sense to Python and allow it to perform the
comparison that makes the while loop work. This while loop ➌ runs as long as
the value of message is not 'quit'.
The first time through the loop, message is just an empty string, so Python enters
the loop. At message = input(prompt), Python displays the prompt and waits for
the user to enter their input. Whatever they enter is assigned to message and
printed; then, Python re evaluates the condition in the while statement. As long as
the user has not entered the word 'quit', the prompt is displayed again and Python
waits for more input. When the user finally enters 'quit', Python stops executing
the while loop and the program ends:
Questions and Answers
1. Create a program that reads the length and width of a farmer’s field from the user in feet.
Display the area of the field in acres.
2. Create a program that reads two integers, a and b, from the user. Your program should
compute and display: the sum of a and b, the difference when b is subtracted from a and
the product of a and b, the quotient when a is divided by b, the remainder when a is
divided by b, the result of log10 of and the result of a to the power of b
Questions and Answers
Questions and Answers