Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
all right everybody
in this topic
I'm going to show you how we can accept user input
in Python we use the input function
it's a function that prompts the user to enter in data
and it returns the enter data as a string
here's an example
to accept user input we will call the input function
when I run this program
we need to enter in data to our console window
like so then hit enter
however we need a prompt
we need to tell the user what we want them to type in
so let's ask a question
our prompt will be within
let's say what is your name
let's try this again
what is your name
I can type in something
why don't you go ahead and type in your full name
then hit enter
now with this input
we're not quite doing anything with it
the input function is going to return some data
as a string
we can assign it to a variable if we would like
let's create a variable of name
name equals our user input
then once we have our name
let's print a message
I'll use an F string
we will print hello
add a placeholder
then insert our name variable within that placeholder
let's try this
what is your name
type in your name
hit enter hello
whatever your name is
let's try a different name
I will pick SpongeBob
many people are familiar with SpongeBob
hello SpongeBob
this time we will ask a user how old they are
let's assign a variable of age equals
accept some user input
we need a prompt within quotes
how old are you
once we have our age variable
let's print I'll use an F string
U r at a placeholder
our variable age
years old
what is your name
type in your name
how old are you
let's say that I'm 25
hello
whatever your name is you are whatever your age is
years old
all right so let's say that it's our birthday
before we print our age variable let's say
happy birthday
since I'm not inserting any variables
within this print statement
this doesn't need to be an F string
you want to use an F string
if you want to insert variables
before we display the user's age
let's take the user's age and increase it by 1
we could say age equals age plus 1
but there's one problem with this
type in a name
how old are you
type in an age
and we have a problem
we have a type error
can only concatenate strings not integers to strings
when we accept user input
we store that input as a string
before we increment our age by 1
we'll need to convert it to an integer
we can't normally use strings
within arithmetic expressions
but we can do that with integers and floats though
after we accept some user input for our age variable
we could take our age variable
and typecast it as an integer
which we talked about in the previous lesson
so let's say age equals our age
after we typecast it
then incremented by one
so type in your name
type in an age
and we get this message
hello your name
happy birthday you are whatever your age is years old
so strings we can't normally use
with arithmetic expressions
we would have to typecast it to an integer or afloat
however we could condense some of these steps
we're taking up an extra line
to typecast our age as an integer
what we could do instead
is that when we accept our user input
we can enclose the input function
within a typecast to INT
and that would work the same
type in your name
type in an age
and this works the same
and it takes less lines of code
and is more readable I would say
when we accept user input
it returns that input as a string data type
then we just have to
typecast it to another data type if we need to
and in this case for age we do
now we'll go over a couple exercises
because it's important to practice what you've Learned
in this first exercise
we're going to calculate the area of a rectangle
we need to prompt the user to enter in a
length and the width of a rectangle
so we will create a variable of length
we will accept some user input using the input function
what is our prompt
let's say enter the length
let's do this with width
I'll just copy and paste what we have
width equals enter the width
so we have the length and the width
to get the area of a rectangle
we have to multiply the length by the width
so let's say area
equals our length variable
now to use multiplication you use an asterisk
we'll discuss different arithmetic
operators in the next lesson
so we have length times width
that is the area
I'm going to print our area
because I need to test something
enter the length let's say 5
5 inches 5 centimeters doesn't matter
enter the width 6
we get a type air
can't multiply sequence by non INT
non integer of type string
when we accept user input it
returns a value of the string data type
we can't use those strings in arithmetic expressions
we're multiplying the length times the width
we would need to typecast them as an integer or afloat
since we're working with basic geometry
such as calculating the area
let's do float
so let's type cast our user input as a float
for both length and width
okay let's try this again
let's say 5 times six
the area that's returned to us is 30
30 point 0 this result contains a decimal
it's a floating point number a float
so when we print the area
I'll use an F string this time
the area is
I'll add a placeholder
display our area variable
let's add a unit of measurement afterwards
I'll pick centimeters
now since we're working with areas
if we would like to technically be accurate
so we could say to the power of two
or we could add a superscript
so if you would like superscript 2
and you're on Windows
make sure numlock is on hold alt
then type on the numpad 0178
so we have a superscript of 2
again it's not really necessary for this lesson
I just think it'd be cool to include it
because then it's technically accurate
all right let's say that the length is 6.1
and the width is 7.2
the area is 43.92 cm squared
because we're working with areas
let's cover a second exercise
this time we will create a shopping cart program
exercise 2
we're going to create a shopping cart program
we need three variables an item a price and a quantity
of those items
we will create a variable of item
we will accept some user input
what item would you like to buy
what are we trying to purchase
we'll keep the data type of the user input as a string
then we need a price
what is the price of each item we're buying
use the input function
what is the price
a price should be a floating point number
for example we might have dollars and cents
we need a decimal
so let's type cast our input as a float
then a quantity
we will accept some user input
our prompt will be
how many
would you like
quantities they should be whole numbers
let's typecast our input as an integer
then we will have a total
what's the total that we have to pay
so let's take the price of each item
use an asterisk for multiply r quantity
then let's do a test run
let's print our total
what item would you like to buy let's say a pizza
what is the price
1099
how many would you like
I would like five pizzas
and our total is 54.95
let's say that before we display the total
let's print the following
I'll use an F string
you have bought
insert a placeholder
display our quantity
x
item or items I'll add s
then we will print I'll use an F string again
your total is
display our total
what item would you like to buy
I would like to buy a pizza
what is the price
1099 how many would you like
I would like nine pizzas
they're all for me I'm gonna eat all of them
you have bought 9x pizzas your total is 98.91
let's add a unit of currency before this
pick unit of currency before displaying the total
I'll pick American dollars
I would like to buy pizza
what is the price
nine dollars ninety nine cents
how many would you like I would like a dozen pizzas 12
you have bought 12 x pizzas
your total is 119 dollars and 88 cents
alright everybody
that is how to accept user input in Python
and we've covered a few exercises
in the next topic
we're going to create a Mad Libs game
and that is how to accept user input in Python
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.