Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:01,260 --> 00:00:05,640
As you know from the previous lectures we have defined this function.
1
2
00:00:05,850 --> 00:00:07,660
And here we are calling it.
2
3
00:00:08,050 --> 00:00:12,600
So it checks if the given temperature is greater than seven and it returns a warm
3
4
00:00:12,600 --> 00:00:19,660
if that is true, and cold if the temperature is equal to seven or less than seven that's fine.
4
5
00:00:19,740 --> 00:00:27,240
But what is the use of this function in a real life setting?
Like who is giving this input?
5
6
00:00:27,240 --> 00:00:33,570
You as a programmer you are writing this function for someone else so that they can use it but you don't
6
7
00:00:33,570 --> 00:00:41,400
expect them to edit the Python file like to have an editor like Visual Studio Code or other editors,
7
8
00:00:41,870 --> 00:00:49,470
and then you cannot just tell them to go to the line 7 and change the values and then execute that code
8
9
00:00:49,890 --> 00:00:53,550
to get different outputs depending on the value.
9
10
00:00:53,550 --> 00:01:01,110
So how about prompting the user to enter a value, and then the user types in the value and then we get that
10
11
00:01:01,140 --> 00:01:03,620
value and process it in our function.
11
12
00:01:03,660 --> 00:01:08,110
In that case we get that value and put it in here in the function call.
12
13
00:01:08,130 --> 00:01:09,640
Let's do that.
13
14
00:01:09,720 --> 00:01:16,410
So we've got a function there. We want to get a value from the user. To get a value from the user
14
15
00:01:16,560 --> 00:01:24,120
use the input function, the input function gets an argument and that is the message you want to show
15
16
00:01:24,120 --> 00:01:27,180
to the user on the command line.
16
17
00:01:27,180 --> 00:01:30,960
For example enter "Enter temperature".
17
18
00:01:33,870 --> 00:01:38,170
maybe put a colon there if you like, you can write whatever you like.
18
19
00:01:38,250 --> 00:01:42,270
Let me save this script and execute it.
19
20
00:01:42,480 --> 00:01:50,340
You see now I executed it and this input function what it does is it freezes the execution of a program
20
21
00:01:50,640 --> 00:01:53,850
and waits for user input on the command line.
21
22
00:01:53,850 --> 00:02:03,990
So if I put inputs something here like seven in this case what is going to happen is that this expression
22
23
00:02:03,990 --> 00:02:06,360
here will be equal to seven.
23
24
00:02:06,360 --> 00:02:12,390
However because we didn't do anything here nothing will happen.
If we print out this value
24
25
00:02:15,680 --> 00:02:21,070
for example 7 you'll get printed out the value of this which is 7.
25
26
00:02:21,200 --> 00:02:23,540
So there you can put anything you want.
26
27
00:02:23,960 --> 00:02:24,530
Hi there.
27
28
00:02:25,640 --> 00:02:31,910
And you get "Hi there" printed out. What I would suggest is instead of printing this out you'd first
28
29
00:02:31,910 --> 00:02:40,430
want to put it in a variable, that would make it more readable so user input, and than you can do whatever
29
30
00:02:40,430 --> 00:02:49,870
you want with that variable like lower it, so to print out the lowercase version of that.
30
31
00:02:49,910 --> 00:02:56,040
Hi hi. So you get hi hi with lowercase letters, okay.
31
32
00:02:56,050 --> 00:02:57,550
Back to our function.
32
33
00:02:57,640 --> 00:03:07,300
Let me now try to print out the weather condition function output with a value of, well user inputs, whatever
33
34
00:03:07,300 --> 00:03:11,230
the user enters there as inputs, so I'll execute the program now.
34
35
00:03:11,230 --> 00:03:14,860
The program will ask me with enter temperature.
35
36
00:03:14,980 --> 00:03:21,060
The program will ask me to enter a temperature so the execution is frozen at this point.
36
37
00:03:21,120 --> 00:03:24,210
This line has not been executed yet.
37
38
00:03:24,310 --> 00:03:28,450
It will be executed after we give a value here and press enter.
38
39
00:03:28,690 --> 00:03:31,750
So let me write down six.
39
40
00:03:31,940 --> 00:03:33,150
Let's see what we are going to get
40
41
00:03:33,150 --> 00:03:36,940
this time, we get an error.
41
42
00:03:37,120 --> 00:03:38,800
Let's read the error carefully.
42
43
00:03:38,800 --> 00:03:42,900
It's about file basics.py which is our file, okay.
43
44
00:03:42,970 --> 00:03:45,560
The error happened at first
44
45
00:03:45,610 --> 00:03:46,540
at this line here.
45
46
00:03:47,260 --> 00:03:54,430
So you print weather condition user input. When Python tried to execute this line it couldn't do it and
46
47
00:03:54,490 --> 00:03:55,210
why?
47
48
00:03:55,210 --> 00:03:56,880
Well you have to follow the error.
48
49
00:03:57,070 --> 00:04:02,560
So still line basics.py line 2, at this line.
49
50
00:04:02,620 --> 00:04:12,790
So when Python tried to execute this line, that line called some other things in the script and the
50
51
00:04:12,790 --> 00:04:15,350
error was in this line.
51
52
00:04:15,370 --> 00:04:15,940
Line 2.
52
53
00:04:16,150 --> 00:04:21,540
So when the function call tried to call the function, it tried to execute this line.
53
54
00:04:22,090 --> 00:04:29,800
So if temperature... It tried to compare 5 with 7, and then it got this error when it's tried to compare
54
55
00:04:29,920 --> 00:04:31,150
5 with 7.
55
56
00:04:31,150 --> 00:04:36,810
So when it's tried to execute this line in other words.
See what it says.
56
57
00:04:36,810 --> 00:04:38,230
Type error.
57
58
00:04:38,700 --> 00:04:46,840
This operator is not supported between instances of string and integer, so string and int.
58
59
00:04:47,100 --> 00:04:51,650
Which means this was a string and this was an int.
59
60
00:04:52,020 --> 00:04:59,180
So our user input was 7, was passed in here and that was compared with 7.
60
61
00:04:59,220 --> 00:05:03,600
It seems that our input the user input was a string and that is true.
61
62
00:05:03,600 --> 00:05:12,570
That was the point I was trying to make here because whatever input the user writes in here even if
62
63
00:05:12,570 --> 00:05:14,050
it's a number.
63
64
00:05:14,250 --> 00:05:16,970
Python will actually converted to a string.
64
65
00:05:17,130 --> 00:05:25,050
So 7 in fact will be converted to a string like with quotes or double quotes inside Python but that
65
66
00:05:25,050 --> 00:05:26,560
has an easy fix.
66
67
00:05:26,670 --> 00:05:36,200
What you can do is you can convert this value which is the string 7,
67
68
00:05:36,390 --> 00:05:38,520
you can convert it into a float.
68
69
00:05:38,520 --> 00:05:42,500
Be careful with the parentheses, so float is a function.
69
70
00:05:42,750 --> 00:05:47,260
It has an open parentheses in here and it has a closing parentheses in here.
70
71
00:05:47,260 --> 00:05:57,450
Input is also a function with its own opening and closing parentheses, so no if I do that, enter temperature 5,
71
72
00:05:57,450 --> 00:06:04,250
I'm going to get the correct answer and let me be transparent with you.
72
73
00:06:04,320 --> 00:06:13,220
Let's again input gets user input "Enter some input".
73
74
00:06:13,430 --> 00:06:13,870
Let me now
74
75
00:06:13,880 --> 00:06:26,100
print out that type of user input, execute, enter 6, you'll see that 6 actually is a string.
75
76
00:06:26,120 --> 00:06:37,790
That's why I am converting it into a float, so that 6 this time will be a float actually which looks like
76
77
00:06:37,880 --> 00:06:38,580
this.
77
78
00:06:38,750 --> 00:06:46,880
You can print out the actual number here, the actual convert number, the float version of the number.
78
79
00:06:47,330 --> 00:06:48,980
Execute again.
79
80
00:06:49,560 --> 00:06:52,460
6, so 6.0.
80
81
00:06:52,460 --> 00:06:54,830
You can also use int if you like.
81
82
00:06:59,760 --> 00:07:08,190
6 so you'll get six which is equal to six point zero of course, but floats would be a better choice because
82
83
00:07:08,460 --> 00:07:15,570
I can show you why, if you enter 6.3 for example int will not be able to convert it because
83
84
00:07:15,660 --> 00:07:27,270
that is like doing int 6.3 in double quotes so int will get confused you will not know how
84
85
00:07:27,270 --> 00:07:30,070
to convert that into an integer.
85
86
00:07:30,480 --> 00:07:34,220
It does however convert 6
86
87
00:07:34,260 --> 00:07:41,630
if it was a simple six without a decimal point. Float will convert both.
87
88
00:07:41,640 --> 00:07:46,740
Sorry both 6 and 6.3.
88
89
00:07:48,210 --> 00:07:52,400
And that's how you use user input in Python.
9073
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.