Python Beginner Projects - Morse Code Converter - Python Practice Programs Episode 4

LeMaster Tech
183 followers
28 Video Views·Apr 23, 2025
Prepare for a technical interview, practice building an app or get ready for an upcoming test or quiz with these sample projects! This episode covers creating a morse code converter to integer numbers and vice versa! This is great practice for designing code to model real world scenarios like translations.
This is episode 4 in a Python beginner project series coming from LeMaster Tech.
Be sure to give us a like, drop a comment, or subscribe to the channel for more content like this!
The previous video:
https://www.youtube.com/watch?v=w78teS7fNRw
python download:
https://www.python.org/downloads/
pycharm download for windows:
https://www.jetbrains.com/pycharm/dow...
hello and welcome back to the channel
today's
video will be episode 4 in a beginner
pro
projects for python series coming out of
this channel
and what we'll be looking at today is a
morse code translator that will take a
number
entered as either
des integer values or
entered in morse code and will convert
it to the opposite depending on what the
user wants to do
so if you're unfamiliar with morse code
here's a video a picture i just grabbed
from the internet these are the numbers
one through nine and zero in morse code
and it's um
just coded language but uh what we're
gonna start with and i've already typed
these you don't have to watch me type
this up 10 times but we're going to
start with a table with the numbers 0
through 9
and then we're going to create an
associated morse code table in the same
order that has the five digit
morse code call and then a space sign
and that's how we'll identify in a long
string of numbers spaces between numbers
so first thing you want to do when
you're converting from one thing to
another is create either lists of both
um sets of data
or you can combine them in a dictionary
which is one way we could have done this
but it's actually
for the way i want to demonstrate this
code i'm going to keep them in two lists
and show how you can use that just know
this is a good
place where you could use a dictionary
as well with key value pairs
that are affiliated with each other
so
without any further ado let's get right
into the code the first input we're
going to want to get from the user is
we're going to ask them whether they
want to go from numbers to morse code or
morse code to numbers and so we'll say
numbers to morse and we'll ask them to
enter true because this is really just a
boolean
or morse two
numbers
and we'll say that will be false
and then we'll have enter true false in
there and then the second thing we'll
ask them for is value which will be
input
and we will say
enter value to be
converted
so there we go i mean that's all the
data we need from the operator and the
rest is going to be our code so let's go
ahead and take care of this first
scenario which is
direction equals true
which
you know we've defined as that being the
scenario where
we're going from integer numbers to
morse code and so we'll create a result
variable that's just going to start as
an empty string
and then for
we'll use a for loop to go through every
number in the number that they enter so
for i
in range and the range is going to be
the length
of the input value
so this is going to do whatever we put
in the loop below once for every
character inside of the value that was
created
so hopefully that makes sense and then
we'll go ahead and create an
intermediate variable that i'll call an
indexer
and that's going to be
just a value that's going to be the
integer
of
the value
of i
so we don't really need to grab its
position in the index for this list
because we have kind of a unique list
here where the position in the index is
the integer value
so i'm just trying to show us using both
tables but you actually don't need to
access the numbers table to go from
numbers to morse code because the index
location is exactly the same spot
so then what we're going to do is we're
going to say the result
plus equals to itself and then the morse
code table
and we're going to grab the value at
whatever position the indexer is at
so you could skip a line of code and
just put this exact code inside of the
morse
table index value but i'm trying to show
where the value comes from
we take the value at position i
we make sure it's in an integer and then
we look up where in the morse code that
integer is
and this then we're going to go ahead
and print at the end of this the result
and so let's go ahead and run one where
we just convert from a number to
morse code first and let's make sure
that that runs oh we're looking at the
previous example
so let's go ahead and make sure that
this runs let's say true because that's
the only situation we've planned for and
let's just say one two three it's an
easy one
okay and it's giving us nothing
so let's go ahead and check out what
we've done here
result is equal to an empty string
direction equals true
hmm
say
true
one two three
oh well there's a value
um that shouldn't really have mattered i
mean saying if direction is the exact
same code as saying if direction is
equal to true but
whatever i guess um
so
here we've got enter value to be
converted one two three and we can
reference our table if we've forgotten
but one two three is actually just
however many stars they start off with
so we can see that's working
and cool we've done half of a morse code
converter so let's go ahead and handle
the other half so now we haven't had
direction equals true
and we're going to want the result to
start off as an empty string just like
before
but now what we're going to say is for i
in range
and this time you actually have to split
the value that was entered up
into
six so we're going to have the length
of the value
but then we have to divide that by six
because every character in there is
six characters long right all of the
morse code numbers are five characters
and then a space
so we take the total number of
characters in there
and we divide it by six and that's going
to default to a float that's what
happens when you do division so we'll
make it an integer because ranges need
integers
and so now we're going to use that
indexer variable again
but this time it's actually going to
come from a function python has built in
to look up the index of something
and we're going to go ahead and put in
the value that we've got
times
the value i times 6 and it's going to go
up to
i
times six
plus six and so as we go through this
loop each time we're grabbing the next
string of six characters
um so it's gonna start from zero up to
six but then the next one is going to
start at 6 and go to 12 and remember
about
ranges of indexes they don't count the
end point so 0 to 6 is going to be 0 1 2
3 4 5 but then 6 to 12 is going to be 6
through 11.
so this is how you address a block of 6
characters in an incrementing for loop
and then in the next rung let's do just
like before
and say the result is going to increase
by
now let's just make it a string value
because that's the only way you can add
text like this concatenated
and we are going to want the numbers at
the indexer that we just looked up
and let's go ahead and
print our
result
and one thing we probably should do
you can do this kind of for all
of your boolean variables and that maybe
was linked to why we were getting
trouble before you can specify that
something's a boolean just like you can
specify something's an integer or a
float
and let's go ahead and check this code
out and see if we can go from
morse code to
a
decimal number now so let's say dash
dash dash dash space i think that's zero
dash that might be eight
and i'm pretty sure
that's one so let's see what we get
[Music]
and we've got a mistake so
interesting let's try putting this back
direction equals true
false
and let's do that again
space
[Music]
and
star
space
well it really shouldn't be
looking at
[Music]
rung 10 at all because this is only
checking to see if it's
true
this air doesn't make a lot of sense
i need to be converted
let's try just one character spend too
much time okay well that worked
um
zero eight okay let's run it true again
see if we can convert a number to morse
well that's not going to work because i
spelled true wrong
stop and rerun
true
enter value to be converted let's say
one two three let's do a long number
is not in the list and now it's looking
at the wrong one with this sam hill
true
one two three four
okay
well
the code is clearly working it's written
correctly to go in both directions for
some reason
having trouble with this boolean input
variable
so
that's fascinating but
in any case
you probably probably what we could do
to solve this would be
just make this a one
and make this
a zero and even though this is basically
the same exact
situation i bet if we say
if direction equals one and now it's
checking against a value
actually we can just put that
in there
let's go ahead and try that real quick
so
one enter value to be converted one two
three seven
we get a value let's try putting a zero
in
and let's do dash dash dash dash space
okay we get a zero so that's a little
frustrating you should be able to use
that as a boolean that shouldn't have
given you an issue but i guess when in
doubt use string inputs um
and i'm super curious if you've
encountered that problem before and you
know a solution uh or you just want to
point out why i'm a dummy go ahead and
let me know in the comments otherwise
hopefully you found this example useful
again you could absolutely create a
dictionary and tie each of these numbers
to a key value pair or the other thing
is you don't really need your numbers
list because you can just use the index
of each number but this is kind of a
unique scenario where that's allowed and
i'm trying to create sort of an idea of
how you can take one list and swap it
out with another list sort of a code
conversion
program so hopefully you found this
useful
feel free to let me know if any comments
about what you saw here and if you found
this or anything else on the channel
useful i'd appreciate uh if you'd leave
a like and a subscribe
and as always i appreciate you checking
out the channel and good luck with your
code thanks bye
