Skip to content

Instantly share code, notes, and snippets.

@DavidHulsman
Created February 28, 2019 18:50
Show Gist options
  • Save DavidHulsman/084ad60e1bf5c984ac2cc0f7ebea0221 to your computer and use it in GitHub Desktop.
Save DavidHulsman/084ad60e1bf5c984ac2cc0f7ebea0221 to your computer and use it in GitHub Desktop.
Thought I'd give /u/Enjgine'd code a refactoring - https://redd.it/avnpue
import os
def cls():
os.system('cls' if os.name == 'nt' else clear)
# moved the first conversion into its own function for easier testing (either manual or automated)
def dist2cm(mode, distance):
# concert all to cm
if mode == m:
distance *= 100
elif mode == inch:
distance *= 2.54
elif mode == foot:
distance *= 30.48
else:
distance *= 1
return distance
def printtarget(target, distance):
# convert to desired output
if target == cm:
print(distance, 'cm')
elif target == m:
distance = float(distance) / 100
print(distance, 'm')
elif target == inch:
distance = float(distance) / 2.54
print(distance, 'inch')
elif target == foot:
distance = float(distance) / 30.48
print(distance, 'foot')
cm = "cm"
m = "m"
inch = "inch"
foot = "foot"
while True:
mode = input('Enter starting unit (cm,m,inch,foot): ')
# moved float() to adhere to DRY - Don't Repeat Yourself :)
distance = float(input('Enter the length you wish to convert: '))
target = input('Enter your target unit (cm,m,inch,foot): ')
distance = dist2cm(mode, distance)
printtarget(target, distance)
# no need for the wait variable, as it's not in use
input("Press enter to continue.")
cls()
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment