Created
January 21, 2019 19:30
-
-
Save chinghwayu/1a2a67e7b87d8177a72f0a9884a86cd7 to your computer and use it in GitHub Desktop.
Simple python REPL command line application with objects, list comprehension, zip, f-strings, and exceptions
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
class ConvertFunctions(): | |
def f2c(self): | |
strtemps = input("Please enter your fahrenheit temperature(s): ") | |
ftemps = [float(ftemp) for ftemp in strtemps.split()] | |
ctemps = [(ftemp-32) * 5/9 for ftemp in ftemps] | |
for (ftemp, ctemp) in zip(ftemps, ctemps): | |
print(f"F:{ftemp:.2f} = C:{ctemp:.2f}") | |
def m2cm(self): | |
strm = input("Please enter the meters: ") | |
lmeters = [int(meter) for meter in strm.split()] | |
cms = [meters*100 for meters in lmeters] | |
for (m, cm) in zip(lmeters, cms): | |
print(f"{m}m = {cm}cm") | |
def quit(self): | |
raise SystemExit | |
def main(): | |
convert = ConvertFunctions() | |
menu_map = { | |
"1" : convert.f2c, | |
"2" : convert.m2cm, | |
"q" : convert.quit, | |
} | |
try: | |
while True: | |
op = input("Please select the conversion you wish to perform\n" | |
"1: Fahrenheit to Celsius\n" | |
"2: Meters to centimetres\n" | |
"q: Exit\n") | |
try: | |
func = menu_map[op] | |
except KeyError: | |
print(f"{op} is not a valid option") | |
else: | |
func() | |
finally: | |
return | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment