1 00:00:00,000 --> 00:00:01,108 [No audio] 2 00:00:01,109 --> 00:00:03,780 As you know from the previous lectures, we have 3 00:00:03,781 --> 00:00:07,828 defined this function and here we are calling it. 4 00:00:07,829 --> 00:00:10,794 So it checks if the given temperature is greater 5 00:00:10,795 --> 00:00:13,252 than 7 and it returns warm if that is 6 00:00:13,253 --> 00:00:16,292 true, and cold if the temperature is equal to 7 00:00:16,293 --> 00:00:19,652 7 or lesser than 7, that's fine. 8 00:00:19,653 --> 00:00:22,324 But what is the use of this 9 00:00:22,325 --> 00:00:25,066 function in a real life setting? 10 00:00:25,067 --> 00:00:27,090 Like, who is giving this input? 11 00:00:27,091 --> 00:00:30,172 You as a programmer, you are writing this function for 12 00:00:30,173 --> 00:00:33,308 someone else so that they can use it, but you 13 00:00:33,309 --> 00:00:37,506 don't expect them to edit the Python file, 14 00:00:37,507 --> 00:00:39,232 like to have an editor like 15 00:00:39,233 --> 00:00:41,686 Visual Studio Code or other editors. 16 00:00:41,687 --> 00:00:44,976 And then you cannot just tell them to go to 17 00:00:44,977 --> 00:00:49,152 line 7 and change the value and then execute that 18 00:00:49,153 --> 00:00:53,412 code to get different outputs depending on the value. 19 00:00:53,413 --> 00:00:57,284 So how about prompting the user to enter a value 20 00:00:57,285 --> 00:00:59,892 and then the user types in the value, and then 21 00:00:59,893 --> 00:01:03,508 we get that value and process it in our function. 22 00:01:03,509 --> 00:01:05,912 In that case, we get that value and 23 00:01:05,913 --> 00:01:08,024 put it in here in the function call. 24 00:01:08,025 --> 00:01:09,510 Let's do that. 25 00:01:09,511 --> 00:01:11,592 So we've got a function there. 26 00:01:11,593 --> 00:01:14,302 We want to get a value from the user. 27 00:01:14,303 --> 00:01:15,692 To get a value from the 28 00:01:15,693 --> 00:01:18,600 user, you use the input function. 29 00:01:19,370 --> 00:01:23,324 The input function gets an argument and that is the message 30 00:01:23,325 --> 00:01:26,988 you want to show to the user on the command line. 31 00:01:26,989 --> 00:01:31,130 For example, Enter temperature. 32 00:01:31,131 --> 00:01:33,566 [Author typing] 33 00:01:33,630 --> 00:01:36,032 Maybe put a colon there if you like. 34 00:01:36,033 --> 00:01:38,080 You can write whatever you like. 35 00:01:38,081 --> 00:01:41,600 Let me save this script and execute it. 36 00:01:42,210 --> 00:01:44,788 You see now I execute it, 37 00:01:44,789 --> 00:01:47,844 and this input function, what it does is 38 00:01:47,845 --> 00:01:50,836 it freezes the execution of a program and 39 00:01:50,837 --> 00:01:53,688 waits for user input on the command line. 40 00:01:53,689 --> 00:01:57,300 So if I put input something here, like seven, 41 00:01:58,230 --> 00:02:02,790 in this case, what's going to happen is that 42 00:02:02,791 --> 00:02:06,268 this expression here will be equal to seven. 43 00:02:06,269 --> 00:02:08,955 However, because we didn't do anything here, nothing 44 00:02:08,956 --> 00:02:12,520 will happen if we print out this value, 45 00:02:12,521 --> 00:02:15,433 [Author typing] 46 00:02:15,450 --> 00:02:18,380 for example, seven, you'll get printed out 47 00:02:18,381 --> 00:02:21,104 the value of this, which is 7. 48 00:02:21,105 --> 00:02:24,176 So there you can put anything you want, Hi 49 00:02:24,177 --> 00:02:27,328 there, and you get Hi there, printed out. 50 00:02:27,329 --> 00:02:30,516 What I would suggest is, instead of printing this 51 00:02:30,517 --> 00:02:34,708 out, you'd first want to put it in a 52 00:02:34,709 --> 00:02:37,690 variable that would make it more readable. 53 00:02:37,691 --> 00:02:39,098 So user_input, 54 00:02:39,099 --> 00:02:40,612 and then you can do whatever you 55 00:02:40,613 --> 00:02:45,540 want with that variable, like lower it. 56 00:02:46,230 --> 00:02:50,578 So to print out the lowercase version of that Hi Hi. 57 00:02:51,110 --> 00:02:54,210 So you get hi hi with lowercase letters. 58 00:02:55,370 --> 00:02:57,564 Okay, back to our function. 59 00:02:57,565 --> 00:03:02,236 Let me now try to print out the weather_condition 60 00:03:02,237 --> 00:03:06,636 function output with a value of, well, user_input, 61 00:03:06,637 --> 00:03:09,686 whatever the user enters as input. 62 00:03:09,687 --> 00:03:11,200 So I'll execute the program 63 00:03:11,201 --> 00:03:14,870 now, the program will ask me with Enter temperature. 64 00:03:14,871 --> 00:03:17,974 The program will ask me to enter a temperature. 65 00:03:17,975 --> 00:03:21,156 So the execution is frozen at this point. 66 00:03:21,157 --> 00:03:24,180 This line has not been executed yet. 67 00:03:24,181 --> 00:03:26,516 It will be executed after we give 68 00:03:26,517 --> 00:03:28,548 a value here and press Enter. 69 00:03:28,549 --> 00:03:31,200 So let me write down 6. 70 00:03:31,730 --> 00:03:33,128 Let's see what we're going to get. 71 00:03:33,129 --> 00:03:36,870 This time we get an error. 72 00:03:36,871 --> 00:03:38,638 Let's read the error carefully. 73 00:03:38,639 --> 00:03:41,982 It's about file basics.py, which is our file. 74 00:03:41,983 --> 00:03:47,068 Okay, the error happened at first at this line here. 75 00:03:47,069 --> 00:03:49,586 So here print(weather_condition(user_input)). 76 00:03:49,587 --> 00:03:51,788 When Python tried to execute this 77 00:03:51,789 --> 00:03:54,010 line, it couldn't do it. 78 00:03:54,011 --> 00:03:55,068 And why? 79 00:03:55,069 --> 00:03:56,898 Well, you have to follow the error. 80 00:03:56,899 --> 00:04:02,480 So steel line basics.py, line 2 at this line. 81 00:04:02,481 --> 00:04:06,240 So when Python tried to execute this line, that 82 00:04:06,241 --> 00:04:12,452 line called some other things in the script and 83 00:04:12,453 --> 00:04:16,084 the error was in this line, line two. 84 00:04:16,085 --> 00:04:18,387 So when the function call tried to call 85 00:04:18,388 --> 00:04:21,812 the function, it tried to execute this line. 86 00:04:21,813 --> 00:04:26,392 So if temperature, it tried to compare 5 with 7. 87 00:04:26,393 --> 00:04:28,232 And then it got this error when 88 00:04:28,233 --> 00:04:31,048 it tried to compare 5 with 7. 89 00:04:31,049 --> 00:04:33,848 So when it tried to execute this line, in 90 00:04:33,849 --> 00:04:37,910 other words, see what it says TypeError. 91 00:04:38,570 --> 00:04:41,372 This operator is not supported between 92 00:04:41,373 --> 00:04:44,730 instances of str and int. 93 00:04:44,731 --> 00:04:48,816 So str and int, which means this was 94 00:04:48,817 --> 00:04:51,100 a string and this was an int. 95 00:04:51,870 --> 00:04:55,478 So our user_input was 7, was passed 96 00:04:55,479 --> 00:04:59,056 in here, and that was compared with 7. 97 00:04:59,057 --> 00:05:02,618 It seems that our input, the user_input was a string. 98 00:05:02,619 --> 00:05:06,564 And that is true. And that was the point I was trying to make here. 99 00:05:06,565 --> 00:05:10,948 Because whatever input the user writes in 100 00:05:10,949 --> 00:05:14,782 here, even if it's a number, Python 101 00:05:14,783 --> 00:05:17,022 will actually convert it to a string. 102 00:05:17,023 --> 00:05:19,352 So 7 in fact, will be converted to a 103 00:05:19,353 --> 00:05:24,430 string like with quotes or double quotes inside Python. 104 00:05:24,431 --> 00:05:26,556 But that has an easy fix. 105 00:05:26,557 --> 00:05:30,633 What you can do is you can convert this 106 00:05:30,634 --> 00:05:32,633 [Author typing] 107 00:05:32,634 --> 00:05:34,252 value, which 108 00:05:34,253 --> 00:05:38,338 is a string seven, you can convert it into a float. 109 00:05:38,339 --> 00:05:40,750 Be careful with parentheses. 110 00:05:40,751 --> 00:05:42,576 So float is a function. 111 00:05:42,577 --> 00:05:44,752 It has an open parentheses in here and 112 00:05:44,753 --> 00:05:47,104 it has a closing parentheses in here. 113 00:05:47,105 --> 00:05:48,928 input is also a function with 114 00:05:48,929 --> 00:05:52,190 its own opening and closing parentheses. 115 00:05:53,250 --> 00:05:57,530 So now if I do that, Enter temperature 116 00:05:57,531 --> 00:06:01,268 5, I'm going to get the correct answer. 117 00:06:01,269 --> 00:06:04,104 And let me be transparent with you. 118 00:06:04,105 --> 00:06:10,142 Let's again input, get user_input. 119 00:06:10,143 --> 00:06:12,370 Enter some input. 120 00:06:13,190 --> 00:06:17,670 Let me now print out the type of user_ input. 121 00:06:19,450 --> 00:06:22,972 Execute, enter 6. 122 00:06:22,973 --> 00:06:26,050 You'll see that 6 actually is a string. 123 00:06:26,051 --> 00:06:31,382 That's why I am converting it into a float. 124 00:06:31,383 --> 00:06:35,296 So that 6 this time will be 125 00:06:35,297 --> 00:06:38,576 a float actually, which looks like this. 126 00:06:38,577 --> 00:06:43,024 You can print out the actual number here, the 127 00:06:43,025 --> 00:06:47,090 actual converted number, the float version of the number. 128 00:06:47,091 --> 00:06:50,324 Execute again, 6. 129 00:06:50,325 --> 00:06:54,923 So 6.0, you can also use Int here if you like. 130 00:06:54,924 --> 00:06:59,766 [Author typing] 131 00:06:59,767 --> 00:07:04,872 6, so you'll get 6, which is equal to 6.0, of course. 132 00:07:04,873 --> 00:07:07,490 But float would be a better choice 133 00:07:07,491 --> 00:07:09,868 because, I can show you why. 134 00:07:09,869 --> 00:07:13,292 If you enter 6.3, for example, int will 135 00:07:13,293 --> 00:07:16,060 not be able to convert it because that 136 00:07:16,061 --> 00:07:23,050 is like doing int("6.3"). 137 00:07:24,510 --> 00:07:26,278 So int will get confused. 138 00:07:26,279 --> 00:07:29,610 It will not know how to convert that into an integer. 139 00:07:30,210 --> 00:07:35,236 It does, however, converts 6 if it was 140 00:07:35,237 --> 00:07:38,682 a simple 6 without a decimal point, float 141 00:07:38,683 --> 00:07:46,400 will convert both sorry, both 6 and 6.3. 142 00:07:48,050 --> 00:07:51,883 And that's how you use user input in Python. 143 00:07:51,884 --> 00:07:55,766 [Outro sound]