Distance, temperature, weight, we're always
converting something from one unit to the other. In today's video we're going to make a simple
unit converter that will allow you to convert all the units you want! Alright guys, here
we are inside Visual Studio Code again, so first of all I'm going to start by printing a new line and then a little title, like that, "Unit Converter".
I'm going to print another line, so now I'm actually going to create
a list of tuples so "conversions", because here we're not actually going to
make a simple like "kilometers to miles" etc etc but we're going to make something
that you can use for all the conversions, so you can actually add conversions to it, you
know, I'm just going to make a few different conversions but you can add as many as
you want so, I'm actually going to copy the list, like that. So here we've got a tuple with
the conversions, so this is the number, so this is actually the unit to convert and
this is what we want to convert to, so this is the same, so this is actually how I set this
up, so, then I'm going to print to the user all the conversions available,
which conversions are available. I'm going to print and then I'm going to
use tuple unpacking, so "conversion_number", "from_unit", "to_unit" in "conversions_available",
"print", then I'm going to use an f-string. Perfect! Let's actually see what we've got so far,
so if everything is working or not. So I'm going to save the file and open a new terminal,
"python unit_converter.py", so as you can see "Unit Converter", "Conversions available", etc
etc so everything is working, perfect! Let's do this. Now, I'm actually going to ask the user
for the conversion to use, so "conversion" is equal to "input", "Enter the number
of the conversion to use", like that and then I'm going to do something like
"conversion_index" equals "int(conversion)" minus 1. So I'm actually subtracting
one to the number entered by the user because if the user entered 1 for example,
which is the first one, the index of this is actually 0, if the user entered 2, the index
would be 1 etc etc so I'm actually going to do that. As usual I'm not going to check if the
user entered a letter or something like that, you should definitely do that
if you want to make the program better, I'm not gonna, as always, check everything
because it's going to make the video too long, so I'm actually assuming that you know the user
actually enters the right number, perfect! Then, I'm actually going to
use tuple unpacking again so, "conversion_number", "from_unit",
"to_unit" and then "conversions_available" and then "conversion_index", so I'm actually
unpacking just the tuple that I need, in this case the tuple at this index. I'm going
to print a little space and then I'm going to do something like "from_value", "float", "input", then f-string "Enter from unit", like that, so here we are actually converting
the input, which is always a string, input always returns a string,
we are converting it to float and here we are telling the user "Enter
kilometers" or stuff like that, perfect! And then "print", so here we're actually
going to add the logic for all the different conversions, so if "conversion_number" is
equal to 1, then "to_value" is equal to "from_value" times 0.62. I'm gonna print, f-string, so "from_value", "from_unit", "to_value", "to_unit". So we're actually printing the result,
let's actually have a look at how this all works and then we can add the other ones, so let's
save it, let's do this and then this. So "python unit_converter.py", "Enter the number of
the conversion to use", I'm going to use the first because it's the only one that we actually added
so 1, "Enter kilometers", let's say 10 kilometers, as you can see 10 kilometers equal
6.2 miles. So this is how it works and that's working really well. Let's actually add
all the others, you can do something like "elif" "conversion_number" equals 2, for example,
let's actually give it some more room, "to_value" equals "from_value" times 1.61, "print", let's actually copy and paste this, like that, so this should work as well
and I'm going to actually copy and paste all the other conversions. So, like this, as you can see we've
got all the different conversions, so we could actually save this
and try to run it and see. So let's say that we want
the sixth, let's say 24.3 Yeah! And here you've got the result, I know this is a little bit too long, because this
is a float and if you don't actually format it in a different way you are going to have this
result but you could actually format strings in a different way, you could remove all of these
zeros and stuff and just keep two decimals after the point, I'm not going to do that,
this is not you know the purpose of this video, the purpose of this video is actually to build
a unit converter and we've actually done that.