In this lesson, we will learn about input functions.
We are going to make our program more dynamic by getting user input! In order to do that, we are going to use the input function!
Let's create some variables:
number 1 and number 2, instead of assigning a static value to our variable, we can make it dynamic with the input function. see below!
With the input function outside the value, we will pass the instructions for the user inside parenthesis with double quotation marks, making it a string (text), i.e., "Please enter the first number: ".
Note that this time we are working with the Python interpreter!
1. Press enter! and it will ask you to type the first number. I chose 4, but you can choose any number of your choice.
2. Type or print the variable number to see the results. We can see that the value 4 has been assigned or stored in the variable number.
Let's do the same for number 2, and then we can perform some basic operations!
You can see that as we added 4 + 8, it gave us 48, and here's why: As I told you earlier, our values are stored as strings (text), so we are dealing with a data type called string. In our next lesson, we will learn more about data types in detail.
Here's how to check the data type of a variable: just use the type function, for instance:
Now we can see that the type of this variable is'str', which means string.
Operators like addition (+) work differently depending on the data type. If we have numbers, the + sign makes a sum of those numbers, but if we have strings, the + sign makes a concatenation of those strings. That's why we just put 4 and 8 together!
Here's something cool we can do with concatenation.
Let's create a variable called first name and last name, assigning a string to each variable.
Now let's use concatenation to create an email address.
I did concatenate my first name and last name with a period (. ), and I concatenated my last name with another string ("@gmail.com").
Type enter to see results!
Remember, with our numbers, we couldn't perform the right operation, and here's how to do it.
We are going to convert our user input from a string to a number.
There are two types of numbers in Python.
1. floats: numbers with decimals. eg. 53.422223
2. integers: numbers without decimals, e.g., 34
For our program, let's allow the user to type decimal numbers in the format the format he wants:
Let's convert the input to a float using the float function.
We converted the variable from text to numbers using the float function.
Now let's get into our program from the IDE.
Copy and replace numbers 1 and 2 from the Python interpreter, and replace them with number one on the IDE. Remember to make the conversion by adding a float function to the value of the variable. Now let's save it and see what we get:
Now our program looks flexible and organised! We can run it now and see if our program is working.
Put in your desired numbers and see if this is the working gas shown above.
Now that our program is dynamic, if we run it again and type different values in the interpreter, here's what we get:
In our next tutorial, we are going to have an exercise. Follow for more! If you have any questions, feel free to leave them in the comment section. Good luck!
This really great start keep it up
ReplyDeleteThanks!
ReplyDelete