Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,872 questions

51,796 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, 2018 by avibootz
...