Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Review one possible solution for the dunder main challenge.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
A final hello to you all.
0:00
Let's take a look at one possible
solution to this challenge.
0:02
In alphabet.py here,
I took this example word_list and
0:07
I moved it down inside of Dunder main,
so that I could use it to test my code.
0:11
I then created a function that I
called first_alpha where I pass in
0:17
the list as a parameter called words.
0:22
And I use the min function to return
the first word alphabetically.
0:25
Now, if you didn't know already, letters
were kind of like numbers in Python.
0:32
So for example,
a B would be greater than an A,
0:37
a D would be greater than a C and so on.
0:40
And I can actually show you that
here inside of the Python console.
0:43
So, if I do B > A, I get it's gonna give
0:48
'B' not defined sorry, "b" > "a".
0:54
And you see I get True.
1:00
So, we can use that to our advantage and
use the minimum function Python
1:03
has built in min function to return
the first word alphabetically.
1:07
So, if I run python3 alphabet.py,
you can see I get bird,
1:12
I'm passing in the word_list
to my function.
1:18
And it's finding the minimum of that list,
which is the word "bird",
1:24
which is the one we were looking for.
1:28
Now, if you used for loops and conditional
statements instead, that's awesome.
1:31
There are so many ways to tackle
challenges in programming, and
1:36
just because my answer is different
than yours, doesn't make it wrong.
1:39
With all that complete,
let's move over into app.py.
1:44
Here I imported the alphabet file and
1:49
then uncommented these two
lines down here at the bottom.
1:51
I also wrapped them in print statements,
because as you can see in my function,
1:55
I chose to do return instead of print.
1:58
So, in order to see
the response in the console,
2:00
I will then have to print out
what my function returns.
2:03
So, from this first word_list,
we should still get bird.
2:08
And then from our last_names list we
should get my last name "Amendola".
2:13
So, if I run python3,
now we do app.py, we get bird,
2:18
and Amendola,
we get the correct responses.
2:23
And you notice, I don't see bird,
printed out to the console twice,
2:27
because I correctly used our
dunder main to practice and
2:32
test out my function
inside of alphabet.py.
2:36
But it doesn't run when I run my script.
2:40
Perfect, nice work Pythonistas.
2:44
As a further challenge if you have another
small Python project, I encourage you to
2:48
try breaking the project up into multiple
files and practice using Dunder main.
2:53
Keep up the awesome work.
2:58
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up