Created
February 28, 2019 18:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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