I'm learning Python currently (love it so far) and have made a little Fahrenheit/Celsius converter.
This is the output upon running it:
Please enter the degrees in Fahrenheit or Celsius to convert: 32
32.0 degrees Celsius is 89.6 degrees Fahrenheit.
32.0 degrees Fahrenheit is 0.0 degrees Celsius.
Do you want to calculate again? (y/n):
Which is how I want it, except if the trailing number after the decimal is a 0 (a whole number), I'd like to drop the .0 entirely (i.e. 5.0 to 5). I'm guessing I'd want an if statement to test if it's equal to zero but how would I go about picking that value?
Full code:
answer = "ERROR"
def calcfc():
""" Calculates F to C and C to F, prints out,
and asks if user wants to run again """
try:
degrees = float(input("\nPlease enter the degrees in Fahrenheit or Celsius to convert: "))
except Exception:
input("\nEnter a valid number next time. Hit enter to terminate.")
exit()
ftoc = (degrees - 32) * 5 / 9
ctof = (degrees * 9) / 5 + 32
print("\n{} degrees Celsius is {:.1f} degrees Fahrenheit.".format(degrees, ctof))
print("{} degrees Fahrenheit is {:.1f} degrees Celsius.".format(degrees, ftoc))
global answer
answer = input("\n\nDo you want to calculate again? (y/n): ")
calcfc()
# run again?
while answer != "y" and answer != "n":
answer = input("\nPlease enter y for yes or n for no: ")
while answer == "y":
calcfc()
if answer == "n":
exit()