0
votes

I'm currently writing a simple temperature conversion program which needs to satisfy the following:

  1. Check if the user entered Celsius or Fahrenheit
  2. Accept either upper or lowercase designation values (i.e. c or C for Celsius)
  3. If the user did not enter Celsius or Fahrenheit, print an error message
  4. Convert to the alternate
  5. 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.")
    

3
A trivial fix would be to compute degree within the if and elif blocks. - Selcuk
Instead of checking it's not "C" or "F" at the end, check at the beginning - bigbounty

3 Answers

0
votes

Put the conversion logic inside the if block

temp = input("Input the  temperature you like to convert? (e.g., 45F, 102C etc.) : ")

if temp.endswith("C") or temp.endswith("F"):
    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.")
0
votes

you can check if user input is valid or invalid like this:

def check_user_input(user_input):
  valid_units = ['c', 'C', 'F', 'f']
  for unit in valid_units:
      if unit in str(user_input) :
         return True    
  return False   

or simply if str(user_input)[-1] not in {'c', 'C', 'f', 'F'}: do something

0
votes

I dont have the ability to comment on @bigbounty's post. So updating with a modified code. You can reduce the number of lines by using this.

You can check for C and F using the temp[-1] directly. Also if you use .upper(), then you will always check for 'C' and 'F'.

Also, now that you have checked for C or F, you know that the condition inside will either be C or F. So you can just use if and else.

temp = input("Input the  temperature you like to convert? (e.g., 45F, 102C etc.) : ")
if temp[-1].upper() in ('C','F'):
    degree = int(temp[:-1]) #all of the string except for its last character

    print("You entered: ", temp)
    print("The degree entry is: ", degree)
    print("The degree type is: ", temp[-1])

    if temp[-1].upper() == 'C':
        result = int(round((9 * degree) / 5 + 32))
        output_type = 'F'
    else: #will be '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.")

And THANK YOU for providing me the opportunity to answer your question. It was fun.