1 00:00:00,590 --> 00:00:07,170 Welcome back. So, Python gets a lot more interesting once we learn how to write code that 2 00:00:07,170 --> 00:00:13,050 doesn't actually execute or output the same thing every single time that we run it. 3 00:00:13,050 --> 00:00:15,000 Now, the first way that we'll learn how to add 4 00:00:15,000 --> 00:00:18,420 some variability into what our code executes, 5 00:00:18,420 --> 00:00:21,345 is by getting input from the user. 6 00:00:21,345 --> 00:00:25,350 So, we'll be able to have different outputs from our code even with 7 00:00:25,350 --> 00:00:27,840 the same program by asking the user for 8 00:00:27,840 --> 00:00:30,900 some input and then doing something with that input. 9 00:00:30,900 --> 00:00:36,390 The way that we'll do that is with the naturally named input function. 10 00:00:36,390 --> 00:00:39,710 So, input asks the user to enter something. 11 00:00:39,710 --> 00:00:45,050 So, for example, in this code on line one we ask the user to input their name, 12 00:00:45,050 --> 00:00:47,060 by calling the input function, 13 00:00:47,060 --> 00:00:50,900 and the input function accepts a prompt as its argument, 14 00:00:50,900 --> 00:00:53,150 and so here we prompt the user, 15 00:00:53,150 --> 00:00:54,755 please enter your name. 16 00:00:54,755 --> 00:00:58,920 Now, the value of this expression of input, 17 00:00:58,920 --> 00:01:02,120 is going to be whatever the user entered. 18 00:01:02,120 --> 00:01:04,130 So, if the user enters Steve, 19 00:01:04,130 --> 00:01:08,660 then the value of this expression is going to be the string Steve. 20 00:01:08,660 --> 00:01:14,045 The value of input is always going to be a string no matter what the user enters. 21 00:01:14,045 --> 00:01:18,605 So, if the user for whatever reason enters 500, 22 00:01:18,605 --> 00:01:23,020 then the value of this expression would be the string 500, 23 00:01:23,020 --> 00:01:26,565 and now n gets assigned either Steve, 24 00:01:26,565 --> 00:01:30,315 500 or whatever string the user entered. 25 00:01:30,315 --> 00:01:32,880 So, when we print out hello n, 26 00:01:32,880 --> 00:01:35,735 then we print out hello and whatever the user entered. 27 00:01:35,735 --> 00:01:38,315 So let's run this code and see what we get. 28 00:01:38,315 --> 00:01:40,280 So, when I hit save and run, 29 00:01:40,280 --> 00:01:42,740 you'll see that I have this prompt here, 30 00:01:42,740 --> 00:01:45,455 and it says please enter your name, 31 00:01:45,455 --> 00:01:48,755 and that's the exact prompt that we passed into input. 32 00:01:48,755 --> 00:01:52,070 So, if I type in Steve, 33 00:01:52,070 --> 00:01:58,295 as my name, then you'll see that my code then prints out hello Steve. 34 00:01:58,295 --> 00:02:02,180 If I save it on my code and I enter something different, so, 35 00:02:02,180 --> 00:02:04,145 let's say that I entered Paul, 36 00:02:04,145 --> 00:02:08,195 then my code is going to output Hello Paul. 37 00:02:08,195 --> 00:02:12,375 I can enter absolutely anything that I want in here. 38 00:02:12,375 --> 00:02:20,075 So, I can enter 99999 and my code is going to print out Hello 999999. 39 00:02:20,075 --> 00:02:25,165 So again, input always gives back a string, 40 00:02:25,165 --> 00:02:28,915 but let's suppose that we wanted a number instead of a string. 41 00:02:28,915 --> 00:02:31,850 The way that we would do that is by casting or 42 00:02:31,850 --> 00:02:35,360 calling one of the functions to convert strings to a number. 43 00:02:35,360 --> 00:02:38,330 So, for example, let's suppose that we wanted to write 44 00:02:38,330 --> 00:02:41,630 some code that was going to convert a number of seconds, 45 00:02:41,630 --> 00:02:44,795 into a number of hours and a number of minutes, 46 00:02:44,795 --> 00:02:47,735 and we want to also compute the remainder 47 00:02:47,735 --> 00:02:51,245 when we actually divide to get the number of hours and minutes. 48 00:02:51,245 --> 00:02:58,175 So, here we prompt the user please enter the number of seconds that you want to convert, 49 00:02:58,175 --> 00:03:03,035 and then we put that into this variable str_seconds. 50 00:03:03,035 --> 00:03:04,580 The next thing that we do, 51 00:03:04,580 --> 00:03:07,400 is we cast that variable str_seconds, 52 00:03:07,400 --> 00:03:10,520 because again input always gives us back a string, 53 00:03:10,520 --> 00:03:12,680 we cast that to be an integer, 54 00:03:12,680 --> 00:03:15,575 and we assign that to be total seconds. 55 00:03:15,575 --> 00:03:19,490 We then compute the number of hours by taking 56 00:03:19,490 --> 00:03:23,460 total seconds and then doing division without remainder, 57 00:03:23,460 --> 00:03:29,330 by 3,600 to convert seconds into hours, 58 00:03:29,330 --> 00:03:33,965 and then we find out how many seconds are still remaining by 59 00:03:33,965 --> 00:03:39,770 taking the remainder of total seconds when it's divided by 3,600. 60 00:03:39,770 --> 00:03:45,185 Then we take those seconds that didn't divide evenly into the number of hours, 61 00:03:45,185 --> 00:03:54,430 and convert them into minutes by again dividing without remainder by 60, 62 00:03:54,430 --> 00:03:59,315 and then we figure out how many seconds are still remaining at the end of this by taking 63 00:03:59,315 --> 00:04:04,485 the remainder when we divide seconds still remaining by 60. 64 00:04:04,485 --> 00:04:07,900 Then in the end we get the number of hours, 65 00:04:07,900 --> 00:04:13,025 minutes and seconds for whatever number of seconds the user input. 66 00:04:13,025 --> 00:04:16,220 So, if I save and run my code, 67 00:04:16,220 --> 00:04:21,440 and then let's suppose that I just want to convert 60 seconds, 68 00:04:21,440 --> 00:04:24,350 then I'll see that this is zero hours, 69 00:04:24,350 --> 00:04:26,855 one minute and zero seconds. 70 00:04:26,855 --> 00:04:31,445 If I convert 6,000 seconds, 71 00:04:31,445 --> 00:04:35,805 then I'll see that this is one hour and 40 minutes. 72 00:04:35,805 --> 00:04:40,300 If I convert 999 seconds, 73 00:04:40,300 --> 00:04:45,345 then I'll see that that's 16 minutes and 39 seconds etc. 74 00:04:45,345 --> 00:04:47,785 So, again the point of this is, 75 00:04:47,785 --> 00:04:52,640 we can use whatever the user entered as an integer by 76 00:04:52,640 --> 00:04:55,130 converting it to an integer by calling 77 00:04:55,130 --> 00:04:58,370 n function or if we wanted to make it a floating point, 78 00:04:58,370 --> 00:05:00,895 we might call it the floating point function. 79 00:05:00,895 --> 00:05:05,000 Now, you might ask what if the user enters something other than an integer? 80 00:05:05,000 --> 00:05:08,720 So, if we're asked to enter the number of seconds that I want to convert, 81 00:05:08,720 --> 00:05:14,660 and I type in let's say my name and I run, 82 00:05:14,660 --> 00:05:17,570 then you'll see that Python wasn't able to 83 00:05:17,570 --> 00:05:21,665 convert Steve into an integer and so we get a runtime error. 84 00:05:21,665 --> 00:05:24,755 Now, let's look at a multiple choice question. 85 00:05:24,755 --> 00:05:29,780 So, this asks, what gets printed when the following statements execute? 86 00:05:29,780 --> 00:05:31,820 So, let's suppose that we ask the user, 87 00:05:31,820 --> 00:05:33,200 please enter your age, 88 00:05:33,200 --> 00:05:35,735 and then the user enters 18. 89 00:05:35,735 --> 00:05:40,325 So, here n gets assigned to the string 18 90 00:05:40,325 --> 00:05:44,640 because remember input always gives us back a string. 91 00:05:44,640 --> 00:05:47,150 It doesn't matter what the user actually entered, 92 00:05:47,150 --> 00:05:49,190 even if they enter 18, 93 00:05:49,190 --> 00:05:53,720 then n gets assigned to the string 18, 94 00:05:53,720 --> 00:05:56,405 and when we print out the type of n, 95 00:05:56,405 --> 00:06:00,035 then we're going to get that n is a string. 96 00:06:00,035 --> 00:06:03,870 That's all for now, until next time.