I'm currently writing a simple temperature conversion program which needs to satisfy the following:
- Check if the user entered Celsius or Fahrenheit
- Accept either upper or lowercase designation values (i.e. c or C for Celsius)
- If the user did not enter Celsius or Fahrenheit, print an error message
- Convert to the alternate
- Print the value, specifying whether it is Celsius or Fahrenheit
currently everything is working fine with the exception of printing out an error message. Say for instance I just enter 0 without a temperature convention. The program just displays: degree = int(temp[:-1]) #all of the string except for its last character ValueError: invalid literal for int() with base 10: ''
What I would like to do is even if I just enter 0 when prompted for an input, it just displays the "Input proper convention" message in the else statement.
The code i'm using:
temp = input("Input the temperature you like to convert? (e.g., 45F, 102C etc.) : ")
degree = int(temp[:-1]) #all of the string except for its last character
input_type = temp[-1] #get the last character
print("You entered: ", temp)
print("The degree entry is: ", degree)
print("The degree type is: ", input_type)
# Add code here
output_type = 0
result = 0
if input_type.upper() == "C":
result = int(round((9 * degree) / 5 + 32))
output_type = "F"
print("The temperature in", output_type, "is", result, "degrees.")
elif input_type.upper() == "F":
result = int(round((degree - 32) * 5 / 9))
output_type = "C"
print("The temperature in", output_type, "is", result, "degrees.")
else:
print("Input proper convention.")
degreewithin theifandelifblocks. - Selcuk