Skip to content

Instantly share code, notes, and snippets.

@chinghwayu
Last active January 15, 2019 16:56
Show Gist options
  • Save chinghwayu/6f7646036e65795d39c35d380d914b76 to your computer and use it in GitHub Desktop.
Save chinghwayu/6f7646036e65795d39c35d380d914b76 to your computer and use it in GitHub Desktop.
Simple python REPL command line application with objects, list comprehension, zip, and f-strings
class ConvertFunctions():
def f2c(self, ftemps):
return [(ftemp-32) * 5/9 for ftemp in ftemps]
def m2cm(self, lmeters):
return [meters*100 for meters in lmeters]
def main():
convert = ConvertFunctions()
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")
if op == "1":
strtemps = input("Please enter your fahrenheit temperature(s): ")
ftemps = [float(ftemp) for ftemp in strtemps.split()]
ctemps = convert.f2c(ftemps)
for (ftemp, ctemp) in zip(ftemps, ctemps):
print(f"F:{ftemp:.2f} = C:{ctemp:.2f}")
elif op == "2":
strm = input("Please enter the meters: ")
meters = [int(meter) for meter in strm.split()]
cms = convert.m2cm(meters)
for (m, cm) in zip(meters, cms):
print(f"{m}m = {cm}cm")
elif op.lower() in {'q', 'quit', 'e', 'exit'}:
return
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment