WEBVTT

1
00:00:00.234 --> 00:00:01.940
<v ->Hi guys, and welcome back!</v>

2
00:00:01.940 --> 00:00:04.700
In this video, we'll learn about getting user input

3
00:00:04.700 --> 00:00:05.910
so that we can get some data

4
00:00:05.910 --> 00:00:08.783
from our users and use it in our programmes.

5
00:00:09.700 --> 00:00:12.200
Let's start off by asking the user for their name,

6
00:00:12.200 --> 00:00:13.510
so we can print it out.

7
00:00:13.510 --> 00:00:16.410
We will say name equals, and again,

8
00:00:16.410 --> 00:00:18.360
this is us defining a variable.

9
00:00:18.360 --> 00:00:20.390
Now, we have to tell the variable what its

10
00:00:20.390 --> 00:00:21.800
value is going to be.

11
00:00:21.800 --> 00:00:23.620
So what we're gonna do here is we're going to

12
00:00:23.620 --> 00:00:26.630
ask the user for their name.

13
00:00:26.630 --> 00:00:28.690
And the way we do that in Python is we type,

14
00:00:28.690 --> 00:00:30.820
input, then a pair of brackets,

15
00:00:30.820 --> 00:00:33.680
and inside the brackets we are gonna type a question

16
00:00:33.680 --> 00:00:36.200
that we'll ask the user or a prompt.

17
00:00:36.200 --> 00:00:40.710
So here, goes a string where we put enter your name.

18
00:00:40.710 --> 00:00:42.650
So here, we're calling another function.

19
00:00:42.650 --> 00:00:44.275
Just like the print function.

20
00:00:44.275 --> 00:00:45.290
But in this case,

21
00:00:45.290 --> 00:00:48.865
this function here will print this prompt out.

22
00:00:48.865 --> 00:00:53.550
And then, it will wait for the user to type and press enter.

23
00:00:53.550 --> 00:00:56.450 line:15% 
Then, we're gonna print out the name.

24
00:00:56.450 --> 00:00:57.860 line:15% 
So, I've run this code here,

25
00:00:57.860 --> 00:01:00.570 line:15% 
and you can see that we get asked to enter our name.

26
00:01:00.570 --> 00:01:02.400 line:15% 
I'm gonna type here, Rolf.

27
00:01:02.400 --> 00:01:04.670 line:15% 
And then, Rolf comes back out.

28
00:01:04.670 --> 00:01:08.350 line:15% 
So, this is how you can ask the user for their name,

29
00:01:08.350 --> 00:01:10.260
or indeed, any other piece of data.

30
00:01:10.260 --> 00:01:12.323
And then, use it in your programmes.

31
00:01:13.370 --> 00:01:16.440
If you wanted to ask the user for a value

32
00:01:16.440 --> 00:01:18.620
that you want to do mathematics on,

33
00:01:18.620 --> 00:01:20.750
then there is something important to take into account

34
00:01:20.750 --> 00:01:22.390
when you're coding in Python.

35
00:01:22.390 --> 00:01:25.740
And that is that the input function always

36
00:01:25.740 --> 00:01:27.710
gives you back a string.

37
00:01:27.710 --> 00:01:30.580
So, this input will ask the user for a value,

38
00:01:30.580 --> 00:01:32.275
and it will give you a string.

39
00:01:32.275 --> 00:01:36.408
Now, remember that you can't do mathematics on strings

40
00:01:36.408 --> 00:01:39.360
in the same way that you can with numbers.

41
00:01:39.360 --> 00:01:41.210
So, if you wanted to calculate the amount

42
00:01:41.210 --> 00:01:43.440
of square metres that is the equivalent

43
00:01:43.440 --> 00:01:45.770
to the size of their house in square feet,

44
00:01:45.770 --> 00:01:49.030
you would do size input, divided by ten point eight.

45
00:01:49.030 --> 00:01:51.210
That's more or less the right number.

46
00:01:51.210 --> 00:01:53.530
And then, you can print square metres.

47
00:01:53.530 --> 00:01:56.410
But, remember that you can't do maths that well.

48
00:01:56.410 --> 00:01:57.620
So, if I save this,

49
00:01:57.620 --> 00:01:59.633
and then I go ahead and run it,

50
00:02:00.662 --> 00:02:05.060 line:15% 
and I ask for example my size is 500 square feet,

51
00:02:05.060 --> 00:02:06.380 line:15% 
you're gonna get an error.

52
00:02:06.380 --> 00:02:09.030 line:15% 
And you can see here that you've got a type error,

53
00:02:09.030 --> 00:02:09.863 line:15% 
and it says,

54
00:02:09.863 --> 00:02:12.790 line:15% 
unsupported operand types for the division

55
00:02:12.790 --> 00:02:14.800 line:15% 
string and float.

56
00:02:14.800 --> 00:02:16.490
So, what this is saying basically is that

57
00:02:16.490 --> 00:02:20.060
we've tried dividing a string by a float,

58
00:02:20.060 --> 00:02:21.800
and you can't do that.

59
00:02:21.800 --> 00:02:25.860
So, we need to convert this into a number

60
00:02:25.860 --> 00:02:27.570
instead of a string.

61
00:02:27.570 --> 00:02:29.162
So, what we've got to do is we have to

62
00:02:29.162 --> 00:02:32.920
first turn this size input into a number.

63
00:02:32.920 --> 00:02:34.240
So, what I'm gonna do is I'm gonna define

64
00:02:34.240 --> 00:02:35.880
a variable called square feet,

65
00:02:35.880 --> 00:02:39.170
and I'm gonna say "int" of size input.

66
00:02:39.170 --> 00:02:40.720
And what this is doing is it's

67
00:02:40.720 --> 00:02:43.230
doing something like what we had before.

68
00:02:43.230 --> 00:02:47.950
We are passing size input into another function,

69
00:02:47.950 --> 00:02:49.530
which is this int.

70
00:02:49.530 --> 00:02:51.860
And that is going to try to turn our string

71
00:02:51.860 --> 00:02:54.290
into an integer, a whole number.

72
00:02:54.290 --> 00:02:56.820
So that we can then do maths on it.

73
00:02:56.820 --> 00:02:59.060
Notice that here in our square metres line,

74
00:02:59.060 --> 00:03:00.520
we're still using size input,

75
00:03:00.520 --> 00:03:02.080
which is our string.

76
00:03:02.080 --> 00:03:04.270
If we wanna make sure that we use the number,

77
00:03:04.270 --> 00:03:07.030
we must use this square feet variable

78
00:03:07.030 --> 00:03:08.940
that we've defined on line two.

79
00:03:08.940 --> 00:03:11.853
Otherwise, that means we're still using the string.

80
00:03:12.970 --> 00:03:15.283
Let's save this and then run it.

81
00:03:16.230 --> 00:03:18.060 line:15% 
So here, we've got this app running,

82
00:03:18.060 --> 00:03:19.520 line:15% 
and I'm gonna type in 500.

83
00:03:19.520 --> 00:03:22.410 line:15% 
And now we get 46.3.

84
00:03:22.410 --> 00:03:24.440
So here's a small challenge for you.

85
00:03:24.440 --> 00:03:26.750
Try to print something a little bit nicer,

86
00:03:26.750 --> 00:03:28.770
rather than just this square metres.

87
00:03:28.770 --> 00:03:30.850
For example, you could print something like,

88
00:03:30.850 --> 00:03:33.300
this number of square feet is equal to this number

89
00:03:33.300 --> 00:03:34.313
of square metres.

90
00:03:35.760 --> 00:03:37.950
I'd recommend pausing the video and giving it a go.

91
00:03:37.950 --> 00:03:40.470
But, I'll try and do it right now as well.

92
00:03:40.470 --> 00:03:41.910
We're gonna use an "f" string.

93
00:03:41.910 --> 00:03:43.510
So I will type the f-key,

94
00:03:43.510 --> 00:03:45.300
and then the double quotation marks,

95
00:03:45.300 --> 00:03:46.133
and inside it,

96
00:03:46.133 --> 00:03:48.383
I'm gonna use my square feet variable,

97
00:03:49.370 --> 00:03:53.555
and type square feet, square feet is square metres,

98
00:03:53.555 --> 00:03:55.373
square metres.

99
00:03:56.960 --> 00:03:59.850
Notice that this here is the variable

100
00:03:59.850 --> 00:04:02.530
that we wanna put the value of inside our string.

101
00:04:02.530 --> 00:04:05.780
So, we still have to say what that variable means.

102
00:04:05.780 --> 00:04:10.780
So, for example, 500 square feet is 46 square metres.

103
00:04:12.490 --> 00:04:14.010 line:15% 
So, if we run this again,

104
00:04:14.010 --> 00:04:16.330 line:15% 
you'll see that as soon I type a 500,

105
00:04:16.330 --> 00:04:17.860 line:15% 
we get a nicer input.

106
00:04:17.860 --> 00:04:20.603 line:15% 
500 square feet is 46 square metres.

107
00:04:21.710 --> 00:04:24.580
If you want to format a floating point number,

108
00:04:24.580 --> 00:04:27.970
so that you only get a certain number of digits at the end,

109
00:04:27.970 --> 00:04:30.590
you can do point two f, for example.

110
00:04:30.590 --> 00:04:32.840
So here, what we've got is a slightly more obscure

111
00:04:32.840 --> 00:04:33.770
piece of syntax.

112
00:04:33.770 --> 00:04:38.770
We've got square metres colon point two f.

113
00:04:38.947 --> 00:04:40.260
And what that does,

114
00:04:40.260 --> 00:04:44.380
is it will try to format this square metres

115
00:04:44.380 --> 00:04:46.580
with two decimal places only.

116
00:04:46.580 --> 00:04:48.900
This is what this piece of syntax means.

117
00:04:48.900 --> 00:04:49.770 line:15% 
So, I'm gonna save that,

118
00:04:49.770 --> 00:04:51.350 line:15% 
run it again,

119
00:04:51.350 --> 00:04:56.350 line:15% 
and now you see we get 46.30 square metres.

120
00:04:56.770 --> 00:04:59.550
If you want to learn more about this formatting syntax,

121
00:04:59.550 --> 00:05:02.030
I'm gonna leave a link in the resources section

122
00:05:02.030 --> 00:05:02.863
of this lecture,

123
00:05:02.863 --> 00:05:04.330
or on the description below,

124
00:05:04.330 --> 00:05:05.898
so that you can have a read of that.

125
00:05:05.898 --> 00:05:07.440
Well, that's everything for this video.

126
00:05:07.440 --> 00:05:09.480
We've learned how to get user input,

127
00:05:09.480 --> 00:05:11.330
and how to turn it into a number,

128
00:05:11.330 --> 00:05:12.986
in case we need to do maths on it.

129
00:05:12.986 --> 00:05:14.690
Thanks for joining me in this one,

130
00:05:14.690 --> 00:05:16.340
and I'll see you in the next one.

