Skip to content

Instantly share code, notes, and snippets.

@chinghwayu
Created January 21, 2019 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chinghwayu/1a2a67e7b87d8177a72f0a9884a86cd7 to your computer and use it in GitHub Desktop.
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
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