Choose the Right Article
00:00 This lesson, we’ll decide whether to use a or an as the article. And this is going to be part of the function in here. Right after picking out all the other random words, you’ll need to write a bit of conditional logic here because you can’t predict beforehand what the first adjective is going to be, because that’s what you have to look at.
00:24 So in the poem schema, you have first article, and then followed by adjective number one. Adjective number one is what we need to focus on. We need to pick the first of the three adjectives that we randomly selected, and then figure out what’s the first character.
00:40 Is it a vowel or not, right? So let me write that down to have a couple of substeps too. We want to pick out the first adjective,
00:51 then get its first character and decide whether it’s a vowel or not. Something like that. I might mush these steps together again to make the code a bit smaller.
01:09 But this is the idea, right? This is the steps that you need to follow. And then depending on whether or not the first letter of the first adjective in the adjectives list is going to be starting with a vowel or not, the article is going to be a or an. Okay?
01:26 Let’s try to cast that into code.
01:30 I want to pick out the first adjective. I do have the adjectives list now. So I want to get the first element in the adjectives list, and I can do that by putting in the index zero.
01:42 I want to go further than that because what I’m actually interested in is just the first character of the first adjective. So I will use double indexing and just continue here with another opening square bracket and another zero, and then closing square bracket.
01:56 This should give me the first character of the first element in the adjectives list. Let’s try it out. I want to also make sure that it works as expected.
02:06 Let’s print out both the adjectives list and the little thing that I did here.
02:15
Okay, I’m going to save and run the code I can get. ferry is the first adjective that got picked out, and indeed adjectives[0][0]
gives me f
.
02:26 So that’s the first letter. Perfect.
02:29 So now I picked out the first adjective and got its first character. I did both of those in one step, basically, by doing this double index notation. And now I want to decide whether it’s a vowel or not.
02:42 So what are the vowels? A, E, I, O, and U. I should be able to just say a conditional statement, if adjectives
02:54
in this string, which is also a collection. So basically adjectives[0][0] in "aeiou"
. That should give me true or false, depending on whether or not the first character is a vowel.
03:09 And then if it is a vowel, then I want the article
03:14
to be "An"
. else
I want the article to be "A"
, so the else
is if it’s not a vowel, if it doesn’t start with a vowel but with a consonant, then I want the article to be "A"
. Okay, let’s try it out.
03:34
I will print adjectives[0]
. I’m going to just print the first adjective, that’s the one we’re interested in, and then also the article,
03:46
and then see whether that matches. I will save and run, and we get fragrant
A
. So A
, and because fragrant
starts with a consonant, it should be A
. That’s correct.
03:57
I’ll run it a few more times until we see whether it also works with a vowel. So there you go. We get incredulous
as the adjective. And then because this starts with an I, which is a vowel, the article now is An
.
04:14
So this is working as well. We decided whether it’s a vowel or not, and we got decide whether to use "A"
or "An"
as the article.
04:27 That’s it. We got all the words that we need to make this schema happen. So in the next lesson, let’s make this schema happen.
Become a Member to join the conversation.