0% found this document useful (0 votes)
46 views

Lecture 11 Interacting With The User

This document discusses user input and while loops in Python. It explains how to use the input() function to prompt users for input and accept both string and numeric values. It demonstrates how to use int() to convert string input to integers for numeric operations. The document also introduces while loops for repeating blocks of code as long as a condition is true, and shows how to use a while loop to let users continuously enter input until they choose to quit. Exercises are provided for users to practice these concepts.

Uploaded by

Alex Kuhudzai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Lecture 11 Interacting With The User

This document discusses user input and while loops in Python. It explains how to use the input() function to prompt users for input and accept both string and numeric values. It demonstrates how to use int() to convert string input to integers for numeric operations. The document also introduces while loops for repeating blocks of code as long as a condition is true, and shows how to use a while loop to let users continuously enter input until they choose to quit. Exercises are provided for users to practice these concepts.

Uploaded by

Alex Kuhudzai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter 11:

Interacting with the User and


Loops
Alex Kuhudzai
[email protected]
Cape Peninsula University of Technology,
Cape Town, South Africa
Numeric Type Basics
 Most programs are written to solve an end user’s problem. To do so, you usually
need to get some information from the user. For example, let’s say someone
wants to find out whether they’re old enough to vote. If you write a program to
answer this question, you need to know the user’s age before you can provide an
answer. The program will need to ask the user to enter, or input, their age; once
the program has this input, it can compare it to the voting age to determine if the
user is old enough and then report the result.
 In this lecture you’ll learn how to accept user input so your program can then
work with it. When your program needs a name, you’ll be able to prompt the user
for a name. When your program needs a list of names, you’ll be able to prompt
the user for a series of names. To do this, you’ll use he input() function. You’ll
also learn how to keep programs running as long as users want them to, so they
can enter as much information as they need to; then, your program can work with
that information. You’ll use Python’s while loop to keep programs running as
long as certain conditions remain true.
 With the ability to work with user input and the ability to control how long your
programs run, you’ll be able to write fully interactive programs.
How the input() Function works
 The input() function pauses your program and waits for the user to enter some
text. Once Python receives the user’s input, it assigns that input to a variable to
make it convenient for you to work with.
 For example, the following program asks the user to enter some text, then
displays that message back to the user:
 PLEASE NOTE THE FOLLOWING TWO SLIDES
How the input() Function works
How the input() Function works
How the input() Function works
How the input() Function works
 The input() function takes one argument: the prompt, or instructions, that we want
to display to the user so they know what to do. In this example, when Python runs
the first line, the user sees the prompt Tell me something, and I will repeat it back
to you: .
 The program waits while the user enters their response and continues after the
user presses ENTER. The response Is assigned to the variable message, then
print(message) displays the input back to the user.
 Each time you use the input() function, you should include a clear, easy-to follow
prompt that tells the user exactly what kind of information you’re looking for.
Using int() to Accept Numerical Input
 When you use the input() function, Python interprets everything the user enters as
a string. Consider the following interpreter session, which asks for the user’s age:

 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 checks if you qualify to compete boxing in a lightweight


division. One has to weigh less than 50kg to compete in the lightweight division.
The programme should ask you for your weight and inform you if you qualify or
not:
 Eg
 YOUR WEIGHT IS *** KGs AND YOU/YOU DON’T QUALIFY TO
COMPETE IN THE LIGHTWEIGHT DIVISION
Do it yourself section (Exercise 1)
 Write a programme that asks the user what kind of rental car they would like.
Print a message about that car , eg
 LET ME SEE IF I CAN FIND YOU A TOYOTA

 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

You might also like