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

1.7 - Input

Uploaded by

ezedean.dahman
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)
21 views

1.7 - Input

Uploaded by

ezedean.dahman
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/ 14

Introduction to Python

Input

ICS 3U0
Today’s Agenda

Using the input function to assign variables


externally
Input
In order to interact with the outside world, a
program needs input.

Any input received from the user must be stored


in a variable.

This is called assigning a value to a variable


externally.
Using the input function
All input assigned to a
Example 1: variable in Python has
data type STRING.

first_name = input("Please enter your


name:")
print("You are:", first_name)
The text in quotes is
called a “prompt” . It
The output will be … must be a STRING.
Using the input function
Example 2:

You need to be careful with the input function. You CANNOT use commas to connect the prompt like
you can with the print function.

 This will work in Python

max_age = 105
min_age = 1
print("Please enter your age. The number you enter should be less than ",
max_age, "and greater than", min_age)

 This will NOT work in Python

max_age = 105
min_age = 1
age = input("Please enter your age. The number you enter should be less
than", max_age, "and greater than", min_age)
print(age)
Example 2 – Correct input function
The input and print functions are different! The input
function is ONLY looking for strings as a prompt AND you cannot
use commas to connect strings. You need to use + and you need
to make sure that everything you are adding together are STRINGS

max_age = 105 Typecasting – changing the


datatype from int to str
min_age = 1
age = input("Please enter your age. The number
you enter should be less than " + str(max_age)
+ " and greater than " + str(min_age) + ": ")
print(age)
Using the input function
Example 3 – You Try!
Write a program that prompts the user
for their name, THEN their age, THEN
their address. Output this information
using one nicely formatted sentence.
Example 3 – Possible Solution
user_name = input("Please enter your
name:")
user_age = input("Please enter your age:")
user_address = input("Please enter your
address:")
print("Hello", user_name, ".")
print("I see you are", user_age, "years
old.")
print("Your address is", user_address,
".")
A nice print option to know …
• As you have probably noticed, using the comma
with the print function automatically puts a
space in between each string. Let’s say you want
to join strings together, but you don’t want any
space between the strings.

• You can use the sep option in the print function


OR you can use the + operator.
Example 3 – Rewritten for nicer output
Using sep option
user_name = input("Please enter your
name:")
user_age = input("Please enter your age:")
user_address = input("Please enter your
address:")
print("Hello ", user_name, ".", sep="")
print("I see you are", user_age, "years
old.")
print("Your address is ", user_address,
".", sep="")
Example 3 – Rewritten for nicer output
Using + operator
user_name = input("Please enter your
name:")
user_age = input("Please enter your age:")
user_address = input("Please enter your
address:")
print("Hello " + user_name + ".")
print("I see you are " + user_age + "years
old.")
print("Your address is " + user_address +
".")
Another nice print option to know …
• If you have a particularly long string to
print, you can separate the text onto
different lines using the end option in the
print function.

• By default, each print statement ends


with a new line. Here, we are changing
that to end with a space instead.
Example 4 – Using end option with long
output
The output for each of these prints is the same. The second
version is just easier to read inside the IDLE window.
print("I think this line is going to be super long. Instead of
writing it all on one line of code, you can split it up using the
end option so that it may be easier to read.")

print("I think this line is going to be super long.", end=" ")


print("Instead of writing it all on one line of code,", end=" ")
print("you can split it up using the end option", end=" ")
print("so that it may be easier to read.", end=" ")
Exercises - Input

Now, complete Exercise 1.7 - Input

You might also like