Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,900 --> 00:00:02,866
all right everybody
2
00:00:02,866 --> 00:00:03,533
in this topic
3
00:00:03,533 --> 00:00:05,700
I'm going to show you how we can accept user input
4
00:00:05,700 --> 00:00:08,000
in Python we use the input function
5
00:00:08,000 --> 00:00:11,500
it's a function that prompts the user to enter in data
6
00:00:11,500 --> 00:00:14,766
and it returns the enter data as a string
7
00:00:14,766 --> 00:00:15,800
here's an example
8
00:00:15,866 --> 00:00:19,000
to accept user input we will call the input function
9
00:00:19,700 --> 00:00:21,100
when I run this program
10
00:00:21,800 --> 00:00:24,466
we need to enter in data to our console window
11
00:00:25,566 --> 00:00:27,500
like so then hit enter
12
00:00:27,800 --> 00:00:29,466
however we need a prompt
13
00:00:30,566 --> 00:00:33,466
we need to tell the user what we want them to type in
14
00:00:34,300 --> 00:00:35,700
so let's ask a question
15
00:00:35,900 --> 00:00:37,366
our prompt will be within
16
00:00:38,266 --> 00:00:41,566
let's say what is your name
17
00:00:44,366 --> 00:00:45,533
let's try this again
18
00:00:45,766 --> 00:00:46,666
what is your name
19
00:00:47,000 --> 00:00:48,366
I can type in something
20
00:00:48,366 --> 00:00:50,466
why don't you go ahead and type in your full name
21
00:00:51,866 --> 00:00:52,800
then hit enter
22
00:00:54,100 --> 00:00:55,366
now with this input
23
00:00:55,366 --> 00:00:57,466
we're not quite doing anything with it
24
00:00:57,600 --> 00:01:00,300
the input function is going to return some data
25
00:01:00,333 --> 00:01:01,366
as a string
26
00:01:01,800 --> 00:01:04,066
we can assign it to a variable if we would like
27
00:01:04,600 --> 00:01:06,600
let's create a variable of name
28
00:01:07,200 --> 00:01:10,133
name equals our user input
29
00:01:11,000 --> 00:01:12,500
then once we have our name
30
00:01:13,400 --> 00:01:14,766
let's print a message
31
00:01:15,100 --> 00:01:16,300
I'll use an F string
32
00:01:16,566 --> 00:01:18,466
we will print hello
33
00:01:18,733 --> 00:01:19,933
add a placeholder
34
00:01:20,466 --> 00:01:23,266
then insert our name variable within that placeholder
35
00:01:24,133 --> 00:01:25,100
let's try this
36
00:01:26,266 --> 00:01:27,100
what is your name
37
00:01:27,400 --> 00:01:28,366
type in your name
38
00:01:28,866 --> 00:01:30,666
hit enter hello
39
00:01:30,800 --> 00:01:31,966
whatever your name is
40
00:01:32,900 --> 00:01:33,900
let's try a different name
41
00:01:34,100 --> 00:01:35,966
I will pick SpongeBob
42
00:01:36,100 --> 00:01:38,066
many people are familiar with SpongeBob
43
00:01:38,466 --> 00:01:39,700
hello SpongeBob
44
00:01:40,466 --> 00:01:43,500
this time we will ask a user how old they are
45
00:01:44,400 --> 00:01:47,600
let's assign a variable of age equals
46
00:01:47,600 --> 00:01:49,466
accept some user input
47
00:01:49,733 --> 00:01:51,700
we need a prompt within quotes
48
00:01:52,800 --> 00:01:55,133
how old are you
49
00:01:57,000 --> 00:01:58,866
once we have our age variable
50
00:01:59,400 --> 00:02:02,200
let's print I'll use an F string
51
00:02:03,100 --> 00:02:05,666
U r at a placeholder
52
00:02:05,800 --> 00:02:07,366
our variable age
53
00:02:09,100 --> 00:02:10,333
years old
54
00:02:12,000 --> 00:02:12,900
what is your name
55
00:02:12,900 --> 00:02:13,733
type in your name
56
00:02:15,500 --> 00:02:16,566
how old are you
57
00:02:17,000 --> 00:02:18,666
let's say that I'm 25
58
00:02:19,900 --> 00:02:20,700
hello
59
00:02:20,866 --> 00:02:24,300
whatever your name is you are whatever your age is
60
00:02:24,466 --> 00:02:25,300
years old
61
00:02:27,066 --> 00:02:29,533
all right so let's say that it's our birthday
62
00:02:30,133 --> 00:02:32,900
before we print our age variable let's say
63
00:02:34,133 --> 00:02:35,966
happy birthday
64
00:02:37,733 --> 00:02:39,900
since I'm not inserting any variables
65
00:02:39,933 --> 00:02:41,300
within this print statement
66
00:02:41,533 --> 00:02:43,533
this doesn't need to be an F string
67
00:02:44,366 --> 00:02:45,500
you want to use an F string
68
00:02:45,500 --> 00:02:47,300
if you want to insert variables
69
00:02:47,466 --> 00:02:49,466
before we display the user's age
70
00:02:50,466 --> 00:02:54,100
let's take the user's age and increase it by 1
71
00:02:54,100 --> 00:02:57,366
we could say age equals age plus 1
72
00:02:58,666 --> 00:03:00,400
but there's one problem with this
73
00:03:01,533 --> 00:03:02,466
type in a name
74
00:03:04,100 --> 00:03:05,133
how old are you
75
00:03:05,133 --> 00:03:06,333
type in an age
76
00:03:07,900 --> 00:03:09,200
and we have a problem
77
00:03:09,300 --> 00:03:10,733
we have a type error
78
00:03:11,300 --> 00:03:15,600
can only concatenate strings not integers to strings
79
00:03:16,466 --> 00:03:18,333
when we accept user input
80
00:03:18,400 --> 00:03:20,900
we store that input as a string
81
00:03:21,300 --> 00:03:23,466
before we increment our age by 1
82
00:03:23,766 --> 00:03:25,666
we'll need to convert it to an integer
83
00:03:25,666 --> 00:03:27,533
we can't normally use strings
84
00:03:27,533 --> 00:03:29,600
within arithmetic expressions
85
00:03:29,800 --> 00:03:32,166
but we can do that with integers and floats though
86
00:03:32,800 --> 00:03:36,166
after we accept some user input for our age variable
87
00:03:36,666 --> 00:03:38,800
we could take our age variable
88
00:03:39,266 --> 00:03:41,666
and typecast it as an integer
89
00:03:42,133 --> 00:03:44,400
which we talked about in the previous lesson
90
00:03:44,933 --> 00:03:48,266
so let's say age equals our age
91
00:03:48,366 --> 00:03:49,900
after we typecast it
92
00:03:50,300 --> 00:03:51,733
then incremented by one
93
00:03:52,933 --> 00:03:54,366
so type in your name
94
00:03:55,800 --> 00:03:57,100
type in an age
95
00:03:58,333 --> 00:03:59,866
and we get this message
96
00:03:59,866 --> 00:04:01,300
hello your name
97
00:04:01,800 --> 00:04:05,900
happy birthday you are whatever your age is years old
98
00:04:06,933 --> 00:04:08,933
so strings we can't normally use
99
00:04:08,933 --> 00:04:10,766
with arithmetic expressions
100
00:04:10,933 --> 00:04:14,000
we would have to typecast it to an integer or afloat
101
00:04:14,333 --> 00:04:16,933
however we could condense some of these steps
102
00:04:17,366 --> 00:04:18,966
we're taking up an extra line
103
00:04:18,966 --> 00:04:21,200
to typecast our age as an integer
104
00:04:21,466 --> 00:04:23,100
what we could do instead
105
00:04:23,500 --> 00:04:25,666
is that when we accept our user input
106
00:04:25,966 --> 00:04:28,000
we can enclose the input function
107
00:04:28,100 --> 00:04:30,266
within a typecast to INT
108
00:04:30,500 --> 00:04:31,766
and that would work the same
109
00:04:32,766 --> 00:04:33,733
type in your name
110
00:04:35,400 --> 00:04:36,600
type in an age
111
00:04:37,366 --> 00:04:38,733
and this works the same
112
00:04:39,500 --> 00:04:41,333
and it takes less lines of code
113
00:04:41,500 --> 00:04:43,133
and is more readable I would say
114
00:04:43,800 --> 00:04:45,366
when we accept user input
115
00:04:45,533 --> 00:04:48,366
it returns that input as a string data type
116
00:04:48,666 --> 00:04:49,400
then we just have to
117
00:04:49,400 --> 00:04:52,133
typecast it to another data type if we need to
118
00:04:52,133 --> 00:04:54,266
and in this case for age we do
119
00:04:54,933 --> 00:04:56,966
now we'll go over a couple exercises
120
00:04:57,133 --> 00:04:59,500
because it's important to practice what you've Learned
121
00:05:00,600 --> 00:05:01,933
in this first exercise
122
00:05:01,933 --> 00:05:04,600
we're going to calculate the area of a rectangle
123
00:05:04,900 --> 00:05:06,933
we need to prompt the user to enter in a
124
00:05:06,933 --> 00:05:09,000
length and the width of a rectangle
125
00:05:09,866 --> 00:05:12,533
so we will create a variable of length
126
00:05:13,166 --> 00:05:16,166
we will accept some user input using the input function
127
00:05:16,900 --> 00:05:18,000
what is our prompt
128
00:05:18,700 --> 00:05:21,300
let's say enter the length
129
00:05:23,166 --> 00:05:24,500
let's do this with width
130
00:05:24,666 --> 00:05:26,466
I'll just copy and paste what we have
131
00:05:27,300 --> 00:05:30,966
width equals enter the width
132
00:05:32,533 --> 00:05:34,400
so we have the length and the width
133
00:05:34,600 --> 00:05:36,366
to get the area of a rectangle
134
00:05:36,366 --> 00:05:38,733
we have to multiply the length by the width
135
00:05:39,133 --> 00:05:40,766
so let's say area
136
00:05:41,266 --> 00:05:43,700
equals our length variable
137
00:05:43,966 --> 00:05:46,733
now to use multiplication you use an asterisk
138
00:05:46,800 --> 00:05:48,266
we'll discuss different arithmetic
139
00:05:48,266 --> 00:05:49,866
operators in the next lesson
140
00:05:50,400 --> 00:05:52,733
so we have length times width
141
00:05:52,866 --> 00:05:54,100
that is the area
142
00:05:55,500 --> 00:05:57,800
I'm going to print our area
143
00:05:58,666 --> 00:06:00,266
because I need to test something
144
00:06:01,200 --> 00:06:03,366
enter the length let's say 5
145
00:06:03,600 --> 00:06:06,300
5 inches 5 centimeters doesn't matter
146
00:06:07,133 --> 00:06:08,600
enter the width 6
147
00:06:09,266 --> 00:06:10,500
we get a type air
148
00:06:10,800 --> 00:06:13,866
can't multiply sequence by non INT
149
00:06:13,866 --> 00:06:16,066
non integer of type string
150
00:06:16,666 --> 00:06:18,400
when we accept user input it
151
00:06:18,566 --> 00:06:21,066
returns a value of the string data type
152
00:06:21,300 --> 00:06:24,333
we can't use those strings in arithmetic expressions
153
00:06:24,666 --> 00:06:27,133
we're multiplying the length times the width
154
00:06:27,533 --> 00:06:31,100
we would need to typecast them as an integer or afloat
155
00:06:31,366 --> 00:06:33,333
since we're working with basic geometry
156
00:06:33,333 --> 00:06:35,066
such as calculating the area
157
00:06:35,533 --> 00:06:36,600
let's do float
158
00:06:37,066 --> 00:06:40,300
so let's type cast our user input as a float
159
00:06:40,366 --> 00:06:42,300
for both length and width
160
00:06:46,600 --> 00:06:48,333
okay let's try this again
161
00:06:49,366 --> 00:06:51,466
let's say 5 times six
162
00:06:52,066 --> 00:06:54,500
the area that's returned to us is 30
163
00:06:54,600 --> 00:06:57,866
30 point 0 this result contains a decimal
164
00:06:57,966 --> 00:07:00,266
it's a floating point number a float
165
00:07:01,400 --> 00:07:03,266
so when we print the area
166
00:07:03,500 --> 00:07:04,966
I'll use an F string this time
167
00:07:05,600 --> 00:07:07,933
the area is
168
00:07:08,733 --> 00:07:10,000
I'll add a placeholder
169
00:07:10,000 --> 00:07:12,000
display our area variable
170
00:07:13,333 --> 00:07:15,300
let's add a unit of measurement afterwards
171
00:07:15,300 --> 00:07:16,566
I'll pick centimeters
172
00:07:16,933 --> 00:07:19,100
now since we're working with areas
173
00:07:19,266 --> 00:07:21,600
if we would like to technically be accurate
174
00:07:21,600 --> 00:07:23,800
so we could say to the power of two
175
00:07:24,566 --> 00:07:26,333
or we could add a superscript
176
00:07:26,400 --> 00:07:28,400
so if you would like superscript 2
177
00:07:28,400 --> 00:07:29,700
and you're on Windows
178
00:07:29,700 --> 00:07:32,600
make sure numlock is on hold alt
179
00:07:32,866 --> 00:07:36,066
then type on the numpad 0178
180
00:07:37,200 --> 00:07:39,100
so we have a superscript of 2
181
00:07:40,066 --> 00:07:42,600
again it's not really necessary for this lesson
182
00:07:42,666 --> 00:07:44,533
I just think it'd be cool to include it
183
00:07:44,533 --> 00:07:46,333
because then it's technically accurate
184
00:07:47,333 --> 00:07:50,333
all right let's say that the length is 6.1
185
00:07:50,966 --> 00:07:53,766
and the width is 7.2
186
00:07:55,266 --> 00:08:00,266
the area is 43.92 cm squared
187
00:08:00,366 --> 00:08:02,166
because we're working with areas
188
00:08:02,600 --> 00:08:04,300
let's cover a second exercise
189
00:08:04,400 --> 00:08:06,900
this time we will create a shopping cart program
190
00:08:07,600 --> 00:08:08,733
exercise 2
191
00:08:08,800 --> 00:08:10,933
we're going to create a shopping cart program
192
00:08:10,933 --> 00:08:15,566
we need three variables an item a price and a quantity
193
00:08:15,566 --> 00:08:16,700
of those items
194
00:08:17,466 --> 00:08:19,800
we will create a variable of item
195
00:08:20,500 --> 00:08:22,266
we will accept some user input
196
00:08:23,966 --> 00:08:28,666
what item would you like to buy
197
00:08:30,166 --> 00:08:31,933
what are we trying to purchase
198
00:08:31,933 --> 00:08:35,000
we'll keep the data type of the user input as a string
199
00:08:36,133 --> 00:08:37,266
then we need a price
200
00:08:37,266 --> 00:08:39,400
what is the price of each item we're buying
201
00:08:40,000 --> 00:08:41,500
use the input function
202
00:08:42,866 --> 00:08:45,600
what is the price
203
00:08:47,666 --> 00:08:49,900
a price should be a floating point number
204
00:08:50,266 --> 00:08:53,166
for example we might have dollars and cents
205
00:08:53,166 --> 00:08:54,266
we need a decimal
206
00:08:54,666 --> 00:08:57,366
so let's type cast our input as a float
207
00:08:58,200 --> 00:08:59,266
then a quantity
208
00:09:01,300 --> 00:09:03,200
we will accept some user input
209
00:09:04,200 --> 00:09:05,466
our prompt will be
210
00:09:06,400 --> 00:09:07,700
how many
211
00:09:08,766 --> 00:09:10,733
would you like
212
00:09:12,900 --> 00:09:15,733
quantities they should be whole numbers
213
00:09:15,866 --> 00:09:18,600
let's typecast our input as an integer
214
00:09:19,866 --> 00:09:21,266
then we will have a total
215
00:09:21,733 --> 00:09:23,766
what's the total that we have to pay
216
00:09:24,566 --> 00:09:26,900
so let's take the price of each item
217
00:09:27,466 --> 00:09:30,966
use an asterisk for multiply r quantity
218
00:09:32,066 --> 00:09:33,400
then let's do a test run
219
00:09:34,500 --> 00:09:35,700
let's print our total
220
00:09:37,766 --> 00:09:40,466
what item would you like to buy let's say a pizza
221
00:09:41,333 --> 00:09:42,500
what is the price
222
00:09:42,766 --> 00:09:44,000
1099
223
00:09:44,933 --> 00:09:46,133
how many would you like
224
00:09:46,166 --> 00:09:48,533
I would like five pizzas
225
00:09:49,600 --> 00:09:52,666
and our total is 54.95
226
00:09:54,300 --> 00:09:56,700
let's say that before we display the total
227
00:09:57,333 --> 00:09:58,766
let's print the following
228
00:09:58,933 --> 00:10:00,100
I'll use an F string
229
00:10:00,700 --> 00:10:02,400
you have bought
230
00:10:03,466 --> 00:10:04,900
insert a placeholder
231
00:10:05,100 --> 00:10:06,733
display our quantity
232
00:10:08,366 --> 00:10:09,166
x
233
00:10:10,600 --> 00:10:14,000
item or items I'll add s
234
00:10:15,900 --> 00:10:18,566
then we will print I'll use an F string again
235
00:10:19,533 --> 00:10:22,133
your total is
236
00:10:23,500 --> 00:10:24,700
display our total
237
00:10:27,333 --> 00:10:28,800
what item would you like to buy
238
00:10:28,866 --> 00:10:30,300
I would like to buy a pizza
239
00:10:31,266 --> 00:10:32,400
what is the price
240
00:10:32,533 --> 00:10:35,500
1099 how many would you like
241
00:10:35,666 --> 00:10:37,866
I would like nine pizzas
242
00:10:38,000 --> 00:10:40,000
they're all for me I'm gonna eat all of them
243
00:10:41,566 --> 00:10:47,466
you have bought 9x pizzas your total is 98.91
244
00:10:47,800 --> 00:10:49,700
let's add a unit of currency before this
245
00:10:50,400 --> 00:10:52,933
pick unit of currency before displaying the total
246
00:10:53,333 --> 00:10:55,000
I'll pick American dollars
247
00:10:56,466 --> 00:10:58,333
I would like to buy pizza
248
00:10:58,766 --> 00:10:59,933
what is the price
249
00:11:00,533 --> 00:11:02,533
nine dollars ninety nine cents
250
00:11:02,866 --> 00:11:06,766
how many would you like I would like a dozen pizzas 12
251
00:11:08,266 --> 00:11:10,733
you have bought 12 x pizzas
252
00:11:11,100 --> 00:11:14,800
your total is 119 dollars and 88 cents
253
00:11:15,700 --> 00:11:16,500
alright everybody
254
00:11:16,500 --> 00:11:19,000
that is how to accept user input in Python
255
00:11:19,000 --> 00:11:21,266
and we've covered a few exercises
256
00:11:21,266 --> 00:11:22,300
in the next topic
257
00:11:22,300 --> 00:11:23,933
we're going to create a Mad Libs game
258
00:11:24,100 --> 00:11:27,133
and that is how to accept user input in Python
17853
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.