hey what's going on everybody it's bro
hope you're doing well and in this video
i'm going to teach you guys how we can
accept some user input in python so
sit back relax and enjoy the show
if you wouldn't mind please like comment
and subscribe
one like equals one prayer for the
youtube algorithm
ladies and gentlemen this is where the
fun begins in this video i'm going to
explain how we can accept some user
input in python
and we do so by using the input function
and then we type in
our input to the console window but it
would probably be good practice to let
the user know what we want them to type
in exactly
so let's ask for somebody's name within
quotes inside the input function i'm
going to type
what is your name and now our program
will wait for us to enter in some user
input so you can use your own name for
this example i'm going to type bro
now to submit some user input you just
type or press
enter now you can see that my process
finished with exit code 0 that means the
program is done running
so we should probably do something with
this value we can actually assign it to
a variable so we can use it elsewhere in
our program
so to do so we precede input with
a variable like name works name equals
input
and when we accept some user input we
will assign it to this variable that we
called them
and now we can do something with it so
let's print a message
involving our name variable that we have
so how about
hello plus name
now when i run this program we can
accept some user input
and then do something with that input
such as display message that says
hello bro and then you can type in
whatever you want for your name
let's say i'm dude what is your name
dude hello dude
now this next part is very important
when we accept user input it is always
of the string data type a series of
characters
what if we need to accept a number well
you can but you can't normally perform
any math on it because it's of the
string data type
we would need to cast it as either of
the integer data type
or of the float data type so here's an
example of why we can't perform any math
on a string let's say we have a variable
called edge
age equals input and let's ask how old
somebody is
how old are you
now i will add one to somebody's age
let's pretend it's their birthday
age equals age plus one this will add
one to their age
now let's try and run this what is your
name bro
how old are you let's say that i'm 21.
okay we ran into an air a type error
can only concatenate string to string
with what i mentioned previously we
cannot normally use
strings for any sort of mathematical
equation or operation
we'll want to cast it to the integer or
the float data type
so let's cast our user input to the
integer data type
and we will surround our user input with
a cast
surround your input with a set of
parenthesis and precede this with int
and now we can use this user input in a
mathematical operation
so let's try it even though this won't
display age yet so
what is your name bro 21 and our program
finished with an x account of zero there
were no problems
now let's attempt to display this age so
i will print
u r plus
age plus years
old we're doing some string
concatenation here
what is your name bro how old are you 21
alright we ran into a type error again
can only concatenate string to string
you cannot normally display a variable
of the integer data type
along with strings because we're doing
string concatenation we're adding
strings together
we would need to convert this variable
back to the string data type
so we can cast it we'll surround our
edge variable with a cast
now this is what's gonna happen we will
accept some user input
it will be of the string data type and
then we will immediately cast it to be
of the integer data type so that we can
treat it
as a number instead of a character and
then if we need to display this age
we will need to convert it back to a
string so that we can use some string
concatenation
and display all of these strings
together and now let's try this theory
so what is your name bro how old are you
let's say i'm 21
hello bro you are 22 years old here's a
situation
what if we enter a number that contains
a decimal portion
so let's try this again let's say i'm
21.5 years old
well now we ran into a different issue a
value error
invalid literal for int with base 10
that means if we cast a string as an
integer
it can only be a whole number a portion
that does not contain
a decimal and that is where the float
data type would come in
it is a data type that can contain a
decimal portion
for this example let's create a third
variable called height and we will ask
for somebody's height
input we'll create a prompt that says
how
tall are you
and we will cast this to be of the float
data type
so that our string our user input can
contain a decimal portion and then we
can treat it
as a number instead of a series of
characters and i think i will delete
this line we won't really need it
anymore
and then let's print their height along
with a message
u r plus then
height but we need to cast this back to
being a string
so we'll surround our height with a cast
plus let's say cm
tall all right let's test this
what is your name bro how old are you
let's say i'm 21 how tall are you let's
pretend that i'm 250.5 centimeters tall
i'm adding the 0.5 just to test to see
if this will accept a floating point
number
hello bro you are 21 years old you are
250.5 centimeters tall
not really all right everybody so that
is the basics of
user input in python normally with user
input it will give you
a value of the string data type if you
need to use that value for any sort of
math
you'll probably need to cast it to be of
the integer or the float data type so if
you would like a copy of this code i
will post this in the comment section
down below
but yeah that's how to accept user input
in python
hey you yeah i'm talking to you if you
learned something new
then help me help you in three easy
steps
by smashing that like button drop a
comment down below
and subscribe if you'd like to become a
fellow bro
[Music]
so
you