1 00:00:00,510 --> 00:00:03,740 Hello and welcome to this video. 2 00:00:04,020 --> 00:00:13,350 When you are programming there will come a time when you want your program to be more interactive and 3 00:00:13,350 --> 00:00:18,500 capture some information from users in Python. 4 00:00:18,510 --> 00:00:25,170 You can achieve this by using a special function called the input function. 5 00:00:25,170 --> 00:00:30,680 This function prompts the user for some information. 6 00:00:30,990 --> 00:00:39,180 What you can then do is capture that information that the user enters and store that information into 7 00:00:39,210 --> 00:00:45,680 a variable and then you can interact with the information the way you want. 8 00:00:45,690 --> 00:00:55,170 I'm going to create a new python file to show you some examples of how to capture your information from 9 00:00:55,440 --> 00:00:56,640 users. 10 00:00:56,640 --> 00:01:04,380 So I'm just going to right click on my main project directory right click and go and you click on python 11 00:01:04,380 --> 00:01:05,500 file. 12 00:01:05,670 --> 00:01:10,110 I'm just going to call these inputs and click OK. 13 00:01:11,010 --> 00:01:22,320 So it's now created the new file called input dot P Y so to capture information from a user you need 14 00:01:22,320 --> 00:01:25,630 to use the input function and what you can do. 15 00:01:25,740 --> 00:01:31,050 You can store the information that you do Capshaw into a variable. 16 00:01:31,050 --> 00:01:41,880 So let's say I create a variable called person and I'm going to set it to increase to an input function. 17 00:01:41,880 --> 00:01:49,380 Now inside that input function I'm just going to add some text that will prompt the user what to do. 18 00:01:49,380 --> 00:01:52,310 I just say enter your name. 19 00:01:53,480 --> 00:02:02,480 So depending on what type of information you're trying to get from the user you can have as many input 20 00:02:02,540 --> 00:02:04,940 functions as required. 21 00:02:04,940 --> 00:02:13,220 For example you can have an input function that will prompt users for their age and other kinds of information 22 00:02:13,250 --> 00:02:19,280 and then store that information into a variable. 23 00:02:19,280 --> 00:02:28,070 So once we have captured that information we may want to output that on the screen and we can do that 24 00:02:28,070 --> 00:02:30,360 using the print function. 25 00:02:30,360 --> 00:02:35,990 So inside the print function we can just add a text. 26 00:02:35,990 --> 00:02:44,750 So what this would do basically once the user gets the prompt to enter the name and then this will print 27 00:02:44,870 --> 00:02:48,030 Hello plus whatever name they have entered. 28 00:02:48,560 --> 00:02:56,940 So I'm just going to save my file and then I'll play this file slightly complete I you can see here 29 00:02:57,060 --> 00:03:02,700 is now giving me the prompt to enter some information. 30 00:03:02,700 --> 00:03:11,340 So I'm gonna send out some information I'm just going to see blue line and when I press enter it's going 31 00:03:11,340 --> 00:03:12,510 to say hello. 32 00:03:12,600 --> 00:03:21,750 Blue line because blue line is the value I have supplied for this variable called person. 33 00:03:21,750 --> 00:03:23,150 So I press enter. 34 00:03:23,430 --> 00:03:25,950 I can see here it says hello. 35 00:03:26,010 --> 00:03:26,640 Blue Line. 36 00:03:27,150 --> 00:03:37,530 So this is just a simple way to capture a user's input so you can have different types of input depending 37 00:03:37,530 --> 00:03:42,240 on the type of data or information you want to get from the user. 38 00:03:42,570 --> 00:03:48,160 You can also capture more Numeric Input from a user. 39 00:03:48,300 --> 00:03:50,940 So let's create some variables. 40 00:03:50,960 --> 00:04:00,060 Well if I created variable X and I'll give it a a value which will be close to an input function. 41 00:04:00,070 --> 00:04:04,470 So I say input because I want to capture the information from the user. 42 00:04:04,620 --> 00:04:12,660 So inside the input function I'm just going to enter a text that says enter and then how create another 43 00:04:12,660 --> 00:04:22,470 variable I'll make this on y and I'll set it again to an input because I want to capture some information 44 00:04:22,560 --> 00:04:24,440 from the user. 45 00:04:24,860 --> 00:04:27,500 Again I I'll say and. 46 00:04:28,720 --> 00:04:29,370 Okay. 47 00:04:29,490 --> 00:04:44,370 And then I can have another variable cause Z and I'll set these two equals two X plus why. 48 00:04:45,460 --> 00:04:46,750 Okay. 49 00:04:46,870 --> 00:04:50,740 And then I can do a print statement. 50 00:04:50,740 --> 00:04:53,690 Print Z. 51 00:04:54,910 --> 00:05:00,600 So when I play this file I want you to watch the outcome carefully. 52 00:05:00,880 --> 00:05:03,760 It's not gonna be the results you expect. 53 00:05:04,360 --> 00:05:09,340 So you would expect an addition to take place. 54 00:05:09,350 --> 00:05:17,500 But what's going to happen is you're going to get a concatenation concatenation is when you add two 55 00:05:17,500 --> 00:05:19,090 strings together. 56 00:05:19,120 --> 00:05:24,870 So let me click on the play button you can see says enter your name. 57 00:05:24,970 --> 00:05:29,770 It's referring to this first print statement on line two. 58 00:05:30,460 --> 00:05:34,320 So I just say and press enter. 59 00:05:34,420 --> 00:05:34,750 All right. 60 00:05:34,760 --> 00:05:35,560 So says hello. 61 00:05:35,560 --> 00:05:36,360 Blue Line. 62 00:05:36,370 --> 00:05:45,970 Now he's gone on to this print statement here on line number eight. 63 00:05:46,000 --> 00:05:49,250 All right so he's basically responding to this. 64 00:05:49,320 --> 00:05:51,430 Says enter a number. 65 00:05:51,430 --> 00:05:57,150 So and number eight a press Enter is asking me to enter another number. 66 00:05:57,150 --> 00:06:02,050 Don't forget I've got two input functions or I in turn number seven. 67 00:06:02,050 --> 00:06:10,180 Now if I press enter you would expect the result to be 15 boys not he's gonna say 87 XY. 68 00:06:10,360 --> 00:06:12,650 It says 87. 69 00:06:12,760 --> 00:06:21,440 And the reason it has said 87 is because it has added the values I have provided. 70 00:06:21,650 --> 00:06:24,780 Together is treated them as a string. 71 00:06:24,790 --> 00:06:28,710 So he thinks they are text and just added them together. 72 00:06:29,140 --> 00:06:33,650 When you add strings together is known as concatenation. 73 00:06:33,670 --> 00:06:41,740 So what we need to do here we need to do some casting so that this value that it captures is converted 74 00:06:41,810 --> 00:06:45,430 in two and a number or an integer. 75 00:06:45,450 --> 00:06:54,820 But in order to convert the values that has been captured from the user in to a number value an Integer 76 00:06:55,300 --> 00:06:57,240 i need to do some casting. 77 00:06:57,250 --> 00:07:00,460 So what I'm going to do here in this variable Z. 78 00:07:00,460 --> 00:07:02,950 This is where I'm going to do the casting. 79 00:07:02,950 --> 00:07:06,600 So what I'm going to do I'm going to wrap this round a print statements. 80 00:07:06,610 --> 00:07:16,780 I'm going to see print and I'm going to wrap that result but I need to include a special function called 81 00:07:16,780 --> 00:07:18,020 The End. 82 00:07:18,040 --> 00:07:28,210 This is what will convert the value that I can show from X and I'll do the same here. 83 00:07:28,390 --> 00:07:31,640 Add this function called INT. 84 00:07:31,750 --> 00:07:39,300 It will convert whatever value that is being captured from Y as well. 85 00:07:39,310 --> 00:07:39,690 All right. 86 00:07:39,700 --> 00:07:44,700 So that will then give us a proper addition. 87 00:07:44,710 --> 00:07:46,250 So I'm going to modify the code. 88 00:07:46,270 --> 00:07:48,010 I'm going to comment out. 89 00:07:48,010 --> 00:07:49,600 Line number eight. 90 00:07:49,690 --> 00:07:59,050 We will not need that anymore since we have ramped the valley of the variable Z inside this print function. 91 00:07:59,050 --> 00:08:00,930 I'm also going to comment out. 92 00:08:00,940 --> 00:08:02,980 Line 1 on line two. 93 00:08:02,980 --> 00:08:04,420 I don't really want that line. 94 00:08:04,420 --> 00:08:06,340 Those line of code to run. 95 00:08:07,090 --> 00:08:10,790 So I just want to keep them simple so we can focus on this area. 96 00:08:10,840 --> 00:08:11,970 So what we will not get. 97 00:08:12,010 --> 00:08:19,210 Now we'll get a prompt to enter a number we enter a number we enter a second number and then in this 98 00:08:19,210 --> 00:08:27,100 verbose Z here to print the number that was entered for X but then converted because when it's entered 99 00:08:27,430 --> 00:08:29,750 the input level it's a string. 100 00:08:29,800 --> 00:08:38,710 So this end here is going to do some casting by converting the captured number into a whole number is 101 00:08:38,710 --> 00:08:40,890 converted from string to number. 102 00:08:40,960 --> 00:08:42,500 It does the same with the Y. 103 00:08:42,850 --> 00:08:48,100 So let me just save my code and then play the code again so we can play. 104 00:08:48,100 --> 00:08:51,070 Now you can see here is asking me to enter a number. 105 00:08:51,070 --> 00:09:00,450 So I say eight and a number eight for example a press Enter says enter another number. 106 00:09:00,730 --> 00:09:08,940 Say I press for five press Enter now it should give me the correct result which is twelve. 107 00:09:09,550 --> 00:09:10,000 Okay. 108 00:09:10,000 --> 00:09:18,250 And the reason is giving me the correct result is because I have done some casting here is in the int 109 00:09:18,280 --> 00:09:27,950 function it converts the value that was entered for the x variable into a whole number. 110 00:09:28,000 --> 00:09:35,440 He also converts the value that was entered for the Y variable into a whole number because any value 111 00:09:35,530 --> 00:09:41,910 entered using just the input function is treated as a string or a text. 112 00:09:41,920 --> 00:09:42,310 All right. 113 00:09:42,340 --> 00:09:49,000 So we have to cost by changing the data type of what was entered. 114 00:09:49,060 --> 00:09:52,400 That's why we were able to get the correct result. 115 00:09:52,560 --> 00:09:54,010 Of twelve. 116 00:09:54,120 --> 00:10:02,100 So that's basically how you can capture your input or information from a user is very useful. 117 00:10:02,100 --> 00:10:09,930 So depending on what type of information you want your program to capture you can use that to do different 118 00:10:09,930 --> 00:10:16,350 things in your program in order to create some kind of interaction with the user. 119 00:10:17,010 --> 00:10:24,390 So that's basically how to capture user information by using the input function. 120 00:10:24,390 --> 00:10:26,450 Thank you for watching and bye for now.