Starting with the basic calculator code created earlier in the series, we will develop a custom module to contain and re-use these functions.
print("#"*60)
print("##"+"Temperature Calculator".center(56)+"##")
print("#"*60+"\n")
print("""Would you like to convert from
F to C, or C to F?\n""")
print("Enter 1 for F to C")
print("Enter 2 for C to F")
strChoice = input("What do you choose?")
if (strChoice == "1"):
strF = input("Enter the temp in F: ")
flF = float(strF)
flC = (flF - 32) / 1.8
print(f"\nThe temp in C is {flC:.2f}.")
elif (strChoice == "2"):
flC = float(input("Enter the temp in C: "))
flF = flC * 1.8 + 32
print(f"\nThe temp in F is {flF:.2f}.")
else:
print("That is not a valid choice.")
print("\nThank you for trying my calculator.")
from myCalc import doFtoC, doCtoF
print("#"*60)
print("##"+"Temperature Calculator".center(56)+"##")
print("#"*60+"\n")
print("""Would you like to convert from
F to C, or C to F?\n""")
print("Enter 1 for F to C")
print("Enter 2 for C to F")
strChoice = input("What do you choose?")
if (strChoice == "1"):
doFtoC()
elif (strChoice == "2"):
doCtoF()
else:
print("That is not a valid choice.")
print("\nThank you for trying my calculator.")
# Custom Module below:
def doFtoC():
strF = input("Enter the temp in F: ")
flF = float(strF)
flC = (flF - 32) / 1.8
print(f"\nThe temp in C is {flC:.2f}.")
def doCtoF():
flC = float(input("Enter the temp in C: "))
flF = flC * 1.8 + 32
print(f"\nThe temp in F is {flF:.2f}.")