Python user input tutorial
#python #user #input
name = input("What is your name?: ")
age = int(input("How old are you?: "))
height = float(input("How tall are you?: "))
print("You are "+str(age)+" years old")
print("You are "+str(height)+"cm tall")
Bro Code merch store: teespring.com/stores/bro-code-5
Up In My Jam (All Of A Sudden) by - Kubbi soundcloud.com/kubbi
Creative Commons — Attribution-ShareAlike 3.0 Unported— CC BY-SA 3.0
Free Download / Stream: bit.ly/2JnDfCE
Music promoted by Audio Library youtu.be/tDexBj46oNI
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, so you.