Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


8,090 questions

11,268 answers

573 users

How to create unit converter (mm, cm, m, km) in Python

1 Answer

0 votes
def unit_converter(value, unit_from, unit_to):
    uc = {'mm': 0.001, 'cm': 0.01, 'm': 1.0, 'km': 1000.}
    return value * uc[unit_from]/uc[unit_to]


print('%.2f' % (unit_converter(1, 'km', 'm')))
print('%.2f' % (unit_converter(1, 'm', 'cm')))
print('%.2f' % (unit_converter(1, 'cm', 'mm')))
print('%.2f' % (unit_converter(1, 'm', 'mm')))
print('%.2f' % (unit_converter(100, 'm', 'km')))


'''
run:
 
1000.00
100.00
10.00
1000.00
0.10
 
'''

 




answered Feb 13 by avibootz
...