All language subtitles for 4. Challenge - Check if it Will Rain in the Next 12 Hours

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German Download
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,450 --> 00:00:03,600 In order to create my rain alert Python script. 1 2 00:00:03,840 --> 00:00:07,590 I want the script to be run every morning at 7:00 AM. 2 3 00:00:08,040 --> 00:00:12,330 Let's say that I'm going to head out of the door at 8:00 AM in order to go to 3 4 00:00:12,330 --> 00:00:16,020 work, I want my script to run at 7:00 AM, 4 5 00:00:16,110 --> 00:00:18,570 check the weather for the next 12 hours, 5 6 00:00:18,720 --> 00:00:23,310 so the time when I'm going to be away from home, and then send me a text message 6 7 00:00:23,370 --> 00:00:24,540 if it's going to rain today 7 8 00:00:24,780 --> 00:00:28,980 so that I can remember to bring an umbrella. That way I'll get notified before I 8 9 00:00:28,980 --> 00:00:31,770 leave home and I'll know how to prepare for the day. 9 10 00:00:33,240 --> 00:00:37,830 The data that we got back from open weather map contains the hourly 10 11 00:00:37,880 --> 00:00:41,810 weather forecast for the next 48 hours. 11 12 00:00:42,260 --> 00:00:45,260 So it starts from zero and goes up to 47. 12 13 00:00:45,800 --> 00:00:49,250 Now we're only interested in the next 12 hours 13 14 00:00:49,340 --> 00:00:53,030 because, if you imagine, this script is run at 7:00 AM 14 15 00:00:53,360 --> 00:00:58,220 then plus 12 hours, that's going to be 7:00 PM. By which time, 15 16 00:00:58,250 --> 00:01:00,200 hopefully I'm already on the way home 16 17 00:01:00,500 --> 00:01:05,000 and I don't need to worry about whether if it rains or not. That's the goal. 17 18 00:01:05,090 --> 00:01:07,700 And if we head back into our code, 18 19 00:01:08,030 --> 00:01:11,600 what we want to do is firstly modify certain parts. 19 20 00:01:12,170 --> 00:01:14,180 Now we know that at the moment, 20 21 00:01:14,240 --> 00:01:18,980 the response code that we're getting back from calling this API is 200. 21 22 00:01:19,010 --> 00:01:22,850 So it's successful. So we're going to call raise for status 22 23 00:01:22,880 --> 00:01:27,880 so that if there is right a problem and we don't get a 200 code that we 23 24 00:01:28,400 --> 00:01:31,370 actually raise an exception. Now, 24 25 00:01:31,400 --> 00:01:36,400 next we want to save our response.json as the weather data. 25 26 00:01:38,120 --> 00:01:41,450 And then we're going to work with this data in order to get the particular 26 27 00:01:41,450 --> 00:01:46,370 pieces that we're interested in. So if we look at this JSON that 27 28 00:01:46,370 --> 00:01:50,630 we get back, firstly, we don't really care about the current weather or 28 29 00:01:50,980 --> 00:01:52,160 the daily weather. 29 30 00:01:52,610 --> 00:01:56,390 So when we look at the API documentation 30 31 00:01:56,780 --> 00:01:58,850 which is probably the hardest part, 31 32 00:01:59,090 --> 00:02:03,020 making sure that you're diligent enough to read the entire documentation, 32 33 00:02:03,380 --> 00:02:07,280 there's often some good nuggets in there. For example, in this case, 33 34 00:02:07,340 --> 00:02:10,610 we can add another parameter called exclude 34 35 00:02:10,970 --> 00:02:15,970 which allows us to exclude some parts of the weather data that comes from the 35 36 00:02:15,980 --> 00:02:20,210 API. This should speed up the API fetching process 36 37 00:02:20,270 --> 00:02:24,800 and it also means that we're transferring less data across the internet. So we 37 38 00:02:24,800 --> 00:02:28,400 can get rid of the current, minutely and daily. 38 39 00:02:28,430 --> 00:02:33,140 We're only interested in the hourly and we have to provide this as a comma- 39 40 00:02:33,140 --> 00:02:37,940 delimited list. So if we take a look at their example, you can see here, 40 41 00:02:37,940 --> 00:02:42,170 they've said exclude and then they've said hourly, daily. 41 42 00:02:42,440 --> 00:02:46,400 And notice how it says without spaces. So as I always say, 42 43 00:02:46,430 --> 00:02:49,100 this is like going into somebody else's house. 43 44 00:02:49,130 --> 00:02:52,550 Everything is different. The way that the washing machine works is different, 44 45 00:02:52,550 --> 00:02:54,560 the way that their dryer works is different. 45 46 00:02:54,830 --> 00:02:59,350 So you really have to look at the API documentation when you were working 46 47 00:02:59,350 --> 00:03:02,110 with a new API just so that you're not caught out 47 48 00:03:02,170 --> 00:03:07,170 and you understand exactly what you have to do in order to be a good API user. 48 49 00:03:09,040 --> 00:03:13,930 Let's go back into our code and let's provide that extra parameter. 49 50 00:03:14,020 --> 00:03:16,630 So that was called exclude. 50 51 00:03:17,560 --> 00:03:22,120 And the thing that we want to exclude is going to be provided as a string. 51 52 00:03:22,630 --> 00:03:25,750 So we want to get rid of current, minutely and daily. 52 53 00:03:25,840 --> 00:03:30,670 So we're going to add that into the string and make sure that we separate each 53 54 00:03:30,700 --> 00:03:32,740 with a comma and without spaces. 54 55 00:03:33,300 --> 00:03:34,133 okay. 55 56 00:03:35,460 --> 00:03:36,930 Now when we hit run 56 57 00:03:37,020 --> 00:03:41,010 and when we get back our weather data and we print it out 57 58 00:03:42,570 --> 00:03:47,550 and we copy this and put it into our online JSON viewer, 58 59 00:03:49,350 --> 00:03:52,170 so let's replace all of the stuff that was there before 59 60 00:03:52,500 --> 00:03:55,560 and you can see the JSON we're getting back is now a lot simpler. 60 61 00:03:55,890 --> 00:03:57,960 We're only getting back the hourly weather 61 62 00:03:58,380 --> 00:03:59,213 forecast. 62 63 00:04:00,930 --> 00:04:02,040 So that's step one done. 63 64 00:04:02,400 --> 00:04:07,400 The next step is to dig through this hourly forecast and get hold of the thing 64 65 00:04:08,280 --> 00:04:12,270 that we're interested in, which is the actual weather condition. 65 66 00:04:13,080 --> 00:04:17,790 The way that weather services tend to provide the weather condition is through 66 67 00:04:17,790 --> 00:04:18,630 an ID. 67 68 00:04:19,200 --> 00:04:22,800 And I know this because I read the API documentation. 68 69 00:04:23,610 --> 00:04:28,230 It's not because I'm some sort of weather geek. Although, I mean, 69 70 00:04:28,230 --> 00:04:29,550 that's not a bad thing to be 70 71 00:04:29,550 --> 00:04:34,110 I guess. If we take a look inside the API key, 71 72 00:04:35,220 --> 00:04:39,930 if we scroll down in this documentation past the examples, 72 73 00:04:40,230 --> 00:04:44,550 you can see it provides all of the fields in the API response. So these are 73 74 00:04:44,550 --> 00:04:48,600 all of the things that we could possibly get back and what they mean. 74 75 00:04:49,050 --> 00:04:54,050 And you can see there's some really interesting things like the UV index or you 75 76 00:04:54,090 --> 00:04:55,170 can get, um, 76 77 00:04:55,200 --> 00:04:59,940 what does the temperature feel like based on the wind chill and actual ground 77 78 00:04:59,940 --> 00:05:00,773 temperature. 78 79 00:05:01,290 --> 00:05:05,040 But what we're mostly interested in is the hourly data. 79 80 00:05:05,460 --> 00:05:08,250 And then inside that the hourly weather data. 80 81 00:05:08,820 --> 00:05:13,820 So this hourly.weather.id is a weather condition id. 81 82 00:05:14,880 --> 00:05:16,290 And when you click on that link, 82 83 00:05:16,320 --> 00:05:21,120 it takes you to this table that shows you all of the weather condition codes 83 84 00:05:21,120 --> 00:05:25,350 that we could possibly get back in this particular field. 84 85 00:05:26,400 --> 00:05:31,400 Now you can see that all the codes that start off with a two means some sort of 85 86 00:05:32,280 --> 00:05:35,130 thunderstorm, and then starting with three 86 87 00:05:35,130 --> 00:05:39,390 that means some sort of drizzling starting, with five means rain, starting with 87 88 00:05:39,390 --> 00:05:43,830 six means snow. And then afterward we have the seven hundreds 88 89 00:05:43,830 --> 00:05:47,400 so these are atmospheric. Thing's like a bit of mist, a bit of smoke, 89 90 00:05:47,670 --> 00:05:51,240 a bit of dust or fog. And this is also incidentally, 90 91 00:05:51,240 --> 00:05:56,240 the reason why this weather key actually has a value that's in the form of 91 92 00:05:57,230 --> 00:06:01,130 a list. You can see that's denoted by the square brackets here. 92 93 00:06:01,610 --> 00:06:06,610 So there could actually be multiple weather conditions for a particular place 93 94 00:06:06,980 --> 00:06:11,420 at a particular hour. And that's because you could maybe have, um, 94 95 00:06:11,450 --> 00:06:16,310 snow, but you could also have fog at the same time. Now, 95 96 00:06:16,310 --> 00:06:20,030 when I looked through a lot of the examples and the documentation, 96 97 00:06:20,330 --> 00:06:24,830 it seems like the first item in that list is the main condition. 97 98 00:06:25,100 --> 00:06:28,550 So if it's going to rain, then it's going to be in that first item 98 99 00:06:28,550 --> 00:06:32,510 in the list of weather conditions. Inside that list, 99 100 00:06:32,540 --> 00:06:35,390 we have a dictionary or many dictionaries. 100 101 00:06:35,990 --> 00:06:38,960 Each of those contain a weather condition ID, 101 102 00:06:39,230 --> 00:06:41,960 the main condition name and the description. 102 103 00:06:42,620 --> 00:06:45,770 So if we look at this ID code 802, 103 104 00:06:45,800 --> 00:06:50,510 we can decode it in this table and you can see it means scattered clouds, 104 105 00:06:50,900 --> 00:06:54,140 25 to 50% of the sky is covered in clouds basically. 105 106 00:06:55,730 --> 00:06:56,960 Based on this list, 106 107 00:06:56,990 --> 00:07:01,580 we can say that well anything that has a code less than 700, 107 108 00:07:01,910 --> 00:07:04,580 then we probably will need an umbrella. 108 109 00:07:05,090 --> 00:07:09,200 I'm not sure how you stand on the umbrella in snow situation 109 110 00:07:09,530 --> 00:07:13,460 but I personally do like to hold an umbrella when it's snowing, 110 111 00:07:13,490 --> 00:07:17,210 especially because I live in a country where the snow is not crazy. 111 112 00:07:17,210 --> 00:07:18,200 It's just sort of, 112 113 00:07:19,580 --> 00:07:24,580 it's never sort of the beautiful snow where it's thick and it gets caught on 113 114 00:07:24,890 --> 00:07:28,100 your eyelashes. It's is the sort of annoying slush 114 115 00:07:28,130 --> 00:07:32,840 that's just sort of snow, but it sort of like somebody spitting at you. 115 116 00:07:33,560 --> 00:07:38,240 So in my case, I would prefer to have an umbrella if it was going to snow. 116 117 00:07:39,020 --> 00:07:43,340 I'm going to check for the codes that we get back from the open weather map 117 118 00:07:43,370 --> 00:07:46,790 API, and if the code is less than 700, 118 119 00:07:47,060 --> 00:07:50,570 then I'm going to advise my user to bring an umbrella. 119 120 00:07:52,490 --> 00:07:54,440 Here's a challenge for you. 120 121 00:07:54,890 --> 00:07:58,940 Can you figure out how to look through the data that we get back, 121 122 00:07:59,000 --> 00:08:03,860 which remember looks something like this, get hold of 122 123 00:08:03,880 --> 00:08:06,130 the first 12 123 124 00:08:06,190 --> 00:08:08,830 items from that hourly list. 124 125 00:08:09,520 --> 00:08:14,520 And then look at the weather and the first item in the weather list and also the 125 126 00:08:16,540 --> 00:08:20,830 ID. So you are going to go all the way down to this, but remember, 126 127 00:08:20,830 --> 00:08:22,900 you're going to do that for all of the items 127 128 00:08:23,410 --> 00:08:26,260 and you're going to check for the condition code. Now, 128 129 00:08:26,290 --> 00:08:31,290 if any of those ID codes are less than 700 then you want to be able to print 129 130 00:08:31,900 --> 00:08:33,280 out, bring an umbrella. 130 131 00:08:33,880 --> 00:08:38,080 So that's the goal, and the actual implementation 131 132 00:08:38,140 --> 00:08:41,050 I'll leave up to you because there's quite a few ways that you can do this. 132 133 00:08:41,350 --> 00:08:46,350 But I'm sure by now you're well prepared with all of your tools in Python to 133 134 00:08:46,450 --> 00:08:51,340 figure this out. If you want to make sure that your code is actually working, 134 135 00:08:51,760 --> 00:08:55,860 you can switch to a latitude and longitude that is definitely raining. 135 136 00:08:56,430 --> 00:08:59,580 So if you go to ventusky.com, 136 137 00:08:59,700 --> 00:09:02,610 they actually show you the live weather forecast. 137 138 00:09:02,940 --> 00:09:06,480 So we can look at precipitation, which is basically rain. 138 139 00:09:06,870 --> 00:09:09,540 And we can find some sort of unfortunate place 139 140 00:09:09,570 --> 00:09:13,320 which seems to be really heavily raining. For example, 140 141 00:09:13,320 --> 00:09:15,300 this place in Poland, Lodz, 141 142 00:09:15,870 --> 00:09:18,960 and if I spell that name correctly, 142 143 00:09:21,890 --> 00:09:22,400 right, 143 144 00:09:22,400 --> 00:09:25,430 Lodz, and then I'll add the country code Poland. 144 145 00:09:26,120 --> 00:09:28,280 And just to make sure in the map 145 146 00:09:28,310 --> 00:09:32,480 it's actually found the correct place. Yep that looks pretty much it. 146 147 00:09:32,840 --> 00:09:37,190 Then I can switch out my latitude and longitude with this rainy place. 147 148 00:09:37,850 --> 00:09:42,770 And that way I know that at least one of the results I get back is definitely 148 149 00:09:42,770 --> 00:09:44,870 going to contain some sort of rain 149 150 00:09:45,380 --> 00:09:46,213 right. 150 151 00:09:47,960 --> 00:09:50,120 Now if we look at their hourly data, 151 152 00:09:50,180 --> 00:09:54,710 you can see that the weather is basically just rain, rain, rain. 152 153 00:09:55,370 --> 00:09:59,600 So between looking at places which are sunny and places 153 154 00:09:59,600 --> 00:10:00,740 which are rainy, 154 155 00:10:00,950 --> 00:10:05,480 you should be able to get your code to work so that it tells you in the next 12 155 156 00:10:05,480 --> 00:10:06,200 hours 156 157 00:10:06,200 --> 00:10:10,730 if any of those condition codes are less than 700, 157 158 00:10:10,910 --> 00:10:13,670 which means it's got some form of precipitation. 158 159 00:10:14,840 --> 00:10:16,550 Pause the video and give this a go. 159 160 00:10:21,170 --> 00:10:21,890 Now. 160 161 00:10:21,890 --> 00:10:24,380 All right. So let's narrow down into this 161 162 00:10:24,410 --> 00:10:29,390 weather. If we want to get the weather for the next hour, 162 163 00:10:29,510 --> 00:10:34,130 then we have to tap into the first item in our hourly list 163 164 00:10:34,520 --> 00:10:36,140 and then we get hold of the weather 164 165 00:10:36,230 --> 00:10:41,180 and then we get hold of the first item in that list. Under the ID key, 165 166 00:10:41,240 --> 00:10:42,560 we'll get the actual value. 166 167 00:10:43,220 --> 00:10:46,040 We've talked about this in detail in previous lessons. 167 168 00:10:46,220 --> 00:10:49,670 If you've skipped a lot of lessons and you've come here directly, 168 169 00:10:50,030 --> 00:10:52,160 then it's going to be a little bit confusing 169 170 00:10:52,250 --> 00:10:55,850 and I recommend to review the previous lessons before you continue. 170 171 00:10:56,780 --> 00:10:59,660 Instead of printing the weather data, let's drill down. 171 172 00:10:59,990 --> 00:11:03,200 Let's get to the first item here, which is hourly. 172 173 00:11:04,010 --> 00:11:08,990 If we provide a set of square brackets and then we can access the value inside 173 174 00:11:08,990 --> 00:11:11,600 the hourly key like this. 174 175 00:11:12,140 --> 00:11:17,140 And now we've got a list with all of the hourly data and it looks pretty much 175 176 00:11:18,050 --> 00:11:19,100 like this. 176 177 00:11:20,450 --> 00:11:25,100 Now you can confirm this by also pasting this into the JSON viewer, 177 178 00:11:25,460 --> 00:11:28,520 replacing the previous text that was there. You can see 178 179 00:11:28,520 --> 00:11:33,520 we now have our 48 items in this list of hourly data. To drill further down, 179 180 00:11:35,210 --> 00:11:40,210 let's get hold of the first item in that list by providing a square bracket 180 181 00:11:40,520 --> 00:11:42,890 and then the index, which is zero. 181 182 00:11:43,580 --> 00:11:48,580 Now we're into the first item and this is what the data looks like. 182 183 00:11:48,680 --> 00:11:49,700 It's a lot shorter. 183 184 00:11:50,540 --> 00:11:53,710 Now we want to tap into the weather condition. 184 185 00:11:54,400 --> 00:11:59,320 So that means yet another set of square brackets and then the name of the key, 185 186 00:11:59,380 --> 00:12:03,280 which is weather. Now, it's pretty simple. 186 187 00:12:03,310 --> 00:12:07,720 It's simply giving us a list with only one item. 187 188 00:12:08,050 --> 00:12:12,100 Let's tap into that one item by using, again, 188 189 00:12:12,370 --> 00:12:13,810 square brackets, zero. 189 190 00:12:14,590 --> 00:12:18,970 And now we've got just a simple dictionary 190 191 00:12:19,000 --> 00:12:21,850 essentially. So if you want to get hold of the ID, 191 192 00:12:21,910 --> 00:12:25,990 then its the final square brackets and the key 192 193 00:12:26,110 --> 00:12:31,110 which is ID. The current weather data ID code is 500 and that, of course, refers to 193 194 00:12:35,770 --> 00:12:36,970 light rain. 194 195 00:12:38,770 --> 00:12:43,770 The next problem is how do we get hold of the first 12 items that we get back in 195 196 00:12:44,380 --> 00:12:48,790 our weather data? In previous lessons, we covered the Python 196 197 00:12:48,790 --> 00:12:49,810 slice function 197 198 00:12:49,840 --> 00:12:54,840 which could work for this by simply providing a value and slicing a sequence 198 199 00:12:55,780 --> 00:13:00,780 like a list or tuple to get hold of a particular section of that list or 199 200 00:13:01,300 --> 00:13:06,100 tuple. Now you can also use the Python slice operator 200 201 00:13:06,130 --> 00:13:08,710 which is the square bracket and a colon 201 202 00:13:09,190 --> 00:13:12,820 and this is probably a more pythonic way of doing things. 202 203 00:13:13,150 --> 00:13:18,010 And you'll see more people doing this in the wild than using the slice function. 203 204 00:13:18,160 --> 00:13:21,640 So let's do it with this notation. We're going to use this version. 204 205 00:13:21,640 --> 00:13:26,640 So we're going to tap into a list and then provide a colon and then where we 205 206 00:13:26,680 --> 00:13:30,550 want to stop because we want to go from the beginning of the list through to 206 207 00:13:30,580 --> 00:13:31,750 stop-1. 207 208 00:13:33,190 --> 00:13:38,190 We know that it's this part of our code that gets us to our list of hourly 208 209 00:13:38,920 --> 00:13:43,420 weather forecasts. So we want to create a slice from that data. 209 210 00:13:44,260 --> 00:13:46,840 So let's copy that and comment this line out. 210 211 00:13:47,290 --> 00:13:50,050 And then I'm going to create my weather slice 211 212 00:13:50,560 --> 00:13:55,560 which is going to be generated from the weather data under the key called 212 213 00:13:55,990 --> 00:13:58,720 hourly. If I print this, 213 214 00:13:58,750 --> 00:14:03,750 you can see it starts off just simply being a list with all of the weather 214 215 00:14:04,120 --> 00:14:07,960 forecasts. Now, if I go ahead and slice this, 215 216 00:14:08,050 --> 00:14:11,920 I'll use that syntax that you saw earlier on where we have a square bracket, 216 217 00:14:12,370 --> 00:14:15,670 a colon, and then where we want our slice to end. 217 218 00:14:16,270 --> 00:14:21,270 And we want this slice to go from zero all the way up to hour 11, 218 219 00:14:22,330 --> 00:14:26,440 so that's 12 hours in total. So if we want to go up to 11, 219 220 00:14:26,470 --> 00:14:30,460 then we have to put 12 in here because it's going to be whatever number here, 220 221 00:14:30,520 --> 00:14:34,330 minus one. And now if we print this data that we get back, 221 222 00:14:34,380 --> 00:14:35,213 and 222 223 00:14:36,630 --> 00:14:40,440 we replaced the existing text inside our JSON viewer, 223 224 00:14:40,800 --> 00:14:41,880 then we take a look at it 224 225 00:14:41,880 --> 00:14:46,880 you can see we've got the next 12 hours of weather data from 0 to 11. 225 226 00:14:49,470 --> 00:14:52,520 Once we've got that slice, the next step is, well, 226 227 00:14:52,520 --> 00:14:54,980 how do we get the rest of the stuff, right? 227 228 00:14:55,400 --> 00:15:00,230 What if I created a new list that contains the condition codes? 228 229 00:15:01,970 --> 00:15:06,970 We know that we've got a list of all of the weather conditions for the next 12 229 230 00:15:07,700 --> 00:15:08,533 hours. 230 231 00:15:08,870 --> 00:15:13,870 How can we loop through that list to find out the actual ID of the weather 231 232 00:15:14,750 --> 00:15:18,500 condition for each of those hours? Well, 232 233 00:15:18,500 --> 00:15:23,500 we can create a for loop that looks at each of the data from the hour, 233 234 00:15:23,960 --> 00:15:27,350 so for hour_data in weather_slice, 234 235 00:15:27,680 --> 00:15:30,410 so this is going to go through each of the 12 hours. 235 236 00:15:30,470 --> 00:15:32,030 And then for each of those hours, 236 237 00:15:32,240 --> 00:15:37,240 we're going to tap into the hour_data and try to get hold of the item that is in 237 238 00:15:38,240 --> 00:15:42,410 the weather key. Like this. 238 239 00:15:43,550 --> 00:15:48,550 Now I'm going to print each of these and you can see we've now got a list of all 239 240 00:15:51,350 --> 00:15:55,400 of the weather conditions for each of those hours. 240 241 00:15:55,940 --> 00:15:58,610 Now let's narrow down that a little bit further. 241 242 00:15:59,000 --> 00:16:02,990 Let's get hold of the first item of each of these lists and you can see, 242 243 00:16:02,990 --> 00:16:05,840 in fact, there's only one item in all of these lists. 243 244 00:16:06,140 --> 00:16:10,490 It's very rare that you have multiple weather conditions for each hour. 244 245 00:16:11,600 --> 00:16:15,950 Now, that gives us a Python dictionary for each of those hours 245 246 00:16:16,340 --> 00:16:21,080 and we can tap into that final value we're interested in under the key id. 246 247 00:16:21,980 --> 00:16:26,980 So now we've looped through the next 12 hours and got hold of the weather 247 248 00:16:28,640 --> 00:16:32,900 condition id for each of those hours. Now, 248 249 00:16:32,930 --> 00:16:37,930 all we need to do is to save this instead of printing it and we'll call it the 249 250 00:16:39,380 --> 00:16:40,520 condition_code. 250 251 00:16:40,960 --> 00:16:41,793 Yeah. 251 252 00:16:44,440 --> 00:16:49,270 And remember previously we said that if the condition code is less than 700, 252 253 00:16:49,540 --> 00:16:52,240 then we're going to print bring an umbrella. 253 254 00:16:53,170 --> 00:16:55,780 So we can check if condition_code, 254 255 00:16:56,140 --> 00:16:59,350 which remember at this point is still a string 255 256 00:16:59,740 --> 00:17:04,740 so we have to turn that into an integer in order to be able to compare it 256 257 00:17:05,230 --> 00:17:06,520 against another number. 257 258 00:17:07,210 --> 00:17:11,410 If that condition_code as an integer is less than 700, 258 259 00:17:11,770 --> 00:17:15,040 then we're going to print bring an umbrella, 259 260 00:17:15,310 --> 00:17:16,143 right? 260 261 00:17:17,740 --> 00:17:20,290 And you can see that for the next 12 hours 261 262 00:17:20,590 --> 00:17:25,360 there's six hours which are going to rain at this particular place 262 263 00:17:25,510 --> 00:17:29,620 in Lodz in Poland that I've put into that latitude and longitude. 263 264 00:17:30,550 --> 00:17:34,840 Now, if I don't want to call this print statement many times, 264 265 00:17:35,410 --> 00:17:37,650 then we can define a variable outside 265 266 00:17:37,670 --> 00:17:42,400 the for loop called will_rain and we can set that to false 266 267 00:17:42,460 --> 00:17:46,360 to begin with. Now, if during the next 12 hours 267 268 00:17:46,360 --> 00:17:51,210 the condition_code is less than 700, then we'll switch that to equal 268 269 00:17:51,390 --> 00:17:53,910 true instead. Now, 269 270 00:17:54,690 --> 00:17:56,550 after the for loop has completed, 270 271 00:17:56,850 --> 00:18:01,440 then we can check to see if it will rain in the next 12 hours. And if so, 271 272 00:18:01,440 --> 00:18:04,980 we'll print bring an umbrella. This way 272 273 00:18:05,010 --> 00:18:09,420 we'll only get one print statement being called instead of every single time we 273 274 00:18:09,420 --> 00:18:12,090 land on a condition code for rain. 274 275 00:18:12,990 --> 00:18:17,990 So this involved quite a bit of JSON passing and also understanding how to work 275 276 00:18:20,190 --> 00:18:22,920 with parameters in APIs. Now, 276 277 00:18:22,950 --> 00:18:25,140 I hope you managed to get this far by yourself. 277 278 00:18:25,200 --> 00:18:29,970 But if not, be sure to review what I've written and fix your code as required. 278 279 00:18:30,420 --> 00:18:31,950 If any of it was confusing, 279 280 00:18:32,010 --> 00:18:35,010 be sure to review previous lessons before you continue 280 281 00:18:35,250 --> 00:18:39,270 because we're now building heavily on your previous knowledge that you learnt 281 282 00:18:39,300 --> 00:18:42,480 through the course. Now in the next lesson, 282 283 00:18:42,510 --> 00:18:45,930 we're going to be looking at how we can, instead of printing 283 284 00:18:45,960 --> 00:18:50,960 bring an umbrella, to send a SMS text message that notifies you to bring an 284 285 00:18:52,650 --> 00:18:56,400 umbrella instead. So for all of that and more, 285 286 00:18:56,700 --> 00:18:57,900 I'll see you on the next lesson. 27805

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.