patternpythonMinor
Unit converter in Python
Viewed 0 times
pythonconverterunit
Problem
I am new to programming and I am trying to make a simple unit converter in python. I want to convert units within the metric system and metric to imperial and vice-versa. I have started with this code and I found this method is slow and in-efficient, How can I code this more efficiently?
import math
import time
"""Unit Converter"""
#Welcome and variable setting
print ("Welcome to Sam's Unit Converter")
cat = raw_input ("Which category would you like to convert? we support length(l) and Weight(w): ")
if cat == ("l"):
unit1 = raw_input ("Which unit would you like to convert from: ")
unit2 = raw_input ("Which unit would you like to convert to: ")
num1 = raw_input ("Enter your value: " )
##Calculations
if unit1 == "cm" and unit2 == "m":
ans = float(num1)/100
elif unit1 == "mm" and unit2 == "cm":
ans = float(num1)/10
elif unit1 == "m" and unit2 == "cm":
ans = float(num1)*100
elif unit1 == "cm" and unit2 == "mm":
ans = float(num1)*10
elif unit1 == "mm" and unit2 == "m":
ans = float(num1)/1000
elif unit1 == "m" and unit2 == "mm":
ans = float(num1)*1000
elif unit1 == "km" and unit2 == "m":
ans = float(num1)*1000
elif unit1 == "m" and unit2 == "km":
ans = float(num1)/1000
elif unit1 == "mm" and unit2 == "km":
ans = float(num1)/1000000Solution
I must respectfully disagree with SuperBiasedMan's recommendation for using a dictionary: while it is already a much better solution than yours, that solution is still making things too complicated.
Instead, I recommend using this:
Above is a chart for converting units within the metric system. And, thanks to a quick google search, a good way to remember this would be to remember the phrase:
King Henry doesn't usually drink chocolate milk.
I have bold-ed the first letter of every word to show the correlation between this and the metric system: the first letter of each word directly corresponds to a metric unit "type": kilo, hecto, etc.
And, again going back to the chart, we can see that there are numbers associated with each unit "type" to show how many of the basic unit is in one of that unit type.
For example, there are 10 basic units in a deka, and 0.01 basic units in a centi.
Now with that information be said, we can easily create a map/different of the different metric "types" and the number of basic units on one of this type.
That would look like this:
To find out what values the user would like, we simply can use the
That would look like this:
Now, for the converting. This process will be very easy since we already have a handy dandy dictionary that holds all the information we need for converting.
All we need to do is divide the amount of basic units of the first type by the amount of basic units for the second type, and then multiply the input number by that result.
Here is what that would look like:
Using this method, we were able to completely reduce all conditionals.
Putting it all together:
If you have an issues or questions, please let me know in a comment.
Instead, I recommend using this:
Above is a chart for converting units within the metric system. And, thanks to a quick google search, a good way to remember this would be to remember the phrase:
King Henry doesn't usually drink chocolate milk.
I have bold-ed the first letter of every word to show the correlation between this and the metric system: the first letter of each word directly corresponds to a metric unit "type": kilo, hecto, etc.
And, again going back to the chart, we can see that there are numbers associated with each unit "type" to show how many of the basic unit is in one of that unit type.
For example, there are 10 basic units in a deka, and 0.01 basic units in a centi.
Now with that information be said, we can easily create a map/different of the different metric "types" and the number of basic units on one of this type.
That would look like this:
types = {
"k": 1000,
"h": 100,
"da": 10,
"": 1,
...
}To find out what values the user would like, we simply can use the
types dictionary and the user input as an indexer to search the dictionary for what values to add.That would look like this:
values[input]Now, for the converting. This process will be very easy since we already have a handy dandy dictionary that holds all the information we need for converting.
All we need to do is divide the amount of basic units of the first type by the amount of basic units for the second type, and then multiply the input number by that result.
Here is what that would look like:
def convert(from_unit_type, to_unit_type, value):
from_type_units = types[from_unit_type]
to_type_units = types[to_unit_type]
new_value = value * (from_type_units / to_type_units)
return str(new_value) + to_unit_typeUsing this method, we were able to completely reduce all conditionals.
Putting it all together:
import math
import time
"""Unit Converter"""
#Welcome and variable setting
types = {
"k": 1000,
"h": 100,
"da": 10,
"": 1,
"d": 0.1,
"c": 0.01,
"m": 0.001
}
def convert(from_unit_type, to_unit_type, value):
from_type_units = types[from_unit_type]
to_type_units = types[to_unit_type]
new_value = value * (from_type_units / to_type_units)
return str(new_value) + to_unit_type
print ("Welcome to Sam's Unit Converter")
cat = raw_input ("Which category would you like to convert? [g,l,m]")
unit1 = raw_input ("Which unit would you like to convert from: ")
unit2 = raw_input ("Which unit would you like to convert to: ")
num1 = raw_input ("Enter your value: " )
print(convert(unit1, unit2, float(num1)))If you have an issues or questions, please let me know in a comment.
Code Snippets
types = {
"k": 1000,
"h": 100,
"da": 10,
"": 1,
...
}values[input]def convert(from_unit_type, to_unit_type, value):
from_type_units = types[from_unit_type]
to_type_units = types[to_unit_type]
new_value = value * (from_type_units / to_type_units)
return str(new_value) + to_unit_typeimport math
import time
"""Unit Converter"""
#Welcome and variable setting
types = {
"k": 1000,
"h": 100,
"da": 10,
"": 1,
"d": 0.1,
"c": 0.01,
"m": 0.001
}
def convert(from_unit_type, to_unit_type, value):
from_type_units = types[from_unit_type]
to_type_units = types[to_unit_type]
new_value = value * (from_type_units / to_type_units)
return str(new_value) + to_unit_type
print ("Welcome to Sam's Unit Converter")
cat = raw_input ("Which category would you like to convert? [g,l,m]")
unit1 = raw_input ("Which unit would you like to convert from: ")
unit2 = raw_input ("Which unit would you like to convert to: ")
num1 = raw_input ("Enter your value: " )
print(convert(unit1, unit2, float(num1)))Context
StackExchange Code Review Q#101348, answer score: 6
Revisions (0)
No revisions yet.