Created
February 28, 2019 18:51
-
-
Save DavidHulsman/25a7cf688409b7fa7ed4d509c84758cd to your computer and use it in GitHub Desktop.
Thought I'd give /u/Enjgine'd code a second 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) | |
# this is a function that returns a dictionary/key-value-pair 'list' | |
# the reason that this isn't a regular variable, as that they can be changed | |
# and Python doesn't have any constants, so you can use a function that returns | |
# a value to emulate constants. | |
# you could transform this to a lambda (once you know what that is and, more | |
# importantly, what it's for), but that's just a style choice | |
def units(): | |
return { | |
'cm': 1, | |
'm': 100, | |
'inch': 2.54, | |
'foot': 30.48 | |
} | |
# you could make this and units() a two-liner if you want, but this is a bit | |
# more readable, IMO | |
def targets(): | |
return { | |
'cm': 1, | |
'm': 100, | |
'inch': 2.54, | |
'foot': 30.48 | |
} | |
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): ') | |
# get the value to convert the distance to centimeters | |
distance *= units().get(mode) | |
# get the value to convert distance to its target | |
distance /= targets().get(target) | |
# print the converted value | |
print(distance, target) | |
# 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