2

I have searched and cannot figure out how this problem arises, or how to solve it. Any help is appreciated, thank you.

my code is the following:

def main():
temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ")
    # algorithm for Farenheit to Kelvin, Celsius, and Rankine
    if temperature[-1] == "F":
    K = (temperature[:-1] + (459.67) * (5.0/9.0))
    print K
    C = (temperature[:-1] - 32) * (5.0/9.0)
    print C
    R = (temperature[:-1] + 459.67)
    print R
    # algorithm for Celsius to Kelvin, Fahrenheit, and Rankine
    elif temperature[-1] == "C":
        K = (temperature[:-1] + 273.15)
        print K
        F = (temperature[:-1] * (9.0/5.0) + 32)
        print F
        R = (temperature[:-1] + 273.15) * (9.0/5.0)
        print R
    # algorithm for Kelvin to Celsius, Fahrenheit, and Rankine
    elif temperature[-1] == "K":
        C = (temperature[:-1] - 273.15)
        print C
        F = (temperature[:-1] * (9.0/5.0) - 459.67)
        print F
        R = (temperature[:-1] * (9.0/5.0))
        print R
    # algorith for Rankine to Fahrenheit, Celsius, and Kelvin
    elif temperature[-1] == "R":
        F = (temperature[:-1] - 459.67)
        print F
        C = (temperature[:-1] - 491.67) * (5.0/9.0)
        print C
        K = (temperature[:-1] * (5.0/9.0))
        print K
main()

and my error message after I enter "50 F":

Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: 50 F

Traceback (most recent call last):
  File "/Users/Minotza/Documents/multi-unit_temperature_converter.py", line 36, in <module>
    main()
  File "/Users/Minotza/Documents/multi-unit_temperature_converter.py", line 5, in main
    K = (temperature[:-1] + (459.67) * (5.0/9.0))
TypeError: cannot concatenate 'str' and 'float' objects
>>> 
1
  • 1
    Turn the string into a float or the float into a string depending on what is desired. Commented Mar 26, 2013 at 3:58

3 Answers 3

3

raw_input returns a string of user-input. What you are looking for is a float. In Python, there is no way to add a string and a float object together. So, what you will have to do is convert the first input into a float so that you can perform mathematical operations on it.

Here's how I would do this:

user_input = raw_input("Prompt Text...").split(' ')

temperature = float(user_input[0])
units_or_option = user_input[1]

So, the split method makes a list out of what the user inputs, using the space character to split up entries. temperature contains the first thing before the space, casted as a float. units_of_option contains the character that is entered after the number and space.

So, if the user enters:

123.5 F

The variables have the values:

user_input = ["123.5", "F"]
temperature = 123.5
units_or_option = "F"
Sign up to request clarification or add additional context in comments.

Comments

2

you require just 2 changes:
1st:
add temperature=float(temperature.split(' ')[0]),temperature.split(' ')[1] just after raw_input for temperature
for example:

temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ")
temperature=float(temperature.split(' ')[0]),temperature.split(' ')[1]
##. . . . your code


2nd:
and replace all temperature[:-1] with temperature[0]



your final code

def main():
    temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ")
    # algorithm for Farenheit to Kelvin, Celsius, and Rankine
    temperature=float(temperature.split(' ')[0]),temperature.split(' ')[1]
    if temperature[-1] == "F":
        K = (temperature[0] + (459.67) * (5.0/9.0))
        print K
        C = (temperature[0] - 32) * (5.0/9.0)
        print C
        R = (temperature[0] + 459.67)
        print R
    # algorithm for Celsius to Kelvin, Fahrenheit, and Rankine
    elif temperature[-1] == "C":
        K = (temperature[0] + 273.15)
        print K
        F = (temperature[0] * (9.0/5.0) + 32)
        print F
        R = (temperature[0] + 273.15) * (9.0/5.0)
        print R
    # algorithm for Kelvin to Celsius, Fahrenheit, and Rankine
    elif temperature[-1] == "K":
        C = (temperature[0] - 273.15)
        print C
        F = (temperature[0] * (9.0/5.0) - 459.67)
        print F
        R = (temperature[0] * (9.0/5.0))
        print R
    # algorith for Rankine to Fahrenheit, Celsius, and Kelvin
    elif temperature[-1] == "R":
        F = (temperature[0] - 459.67)
        print F
        C = (temperature[0] - 491.67) * (5.0/9.0)
        print C
        K = (temperature[0] * (5.0/9.0))
        print K
main()

Comments

1
temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ")

raw_input returns a string, so you want to split the string OR convert it into each step into a float.

It's always better to set the number as a variable instead of calculating it repeatedly, so you can do th

Converting it to floating-point number:

temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ")
temp = float(temperature.split(' ')[0])

What string.split(obj) does is it returns a list of the parts of string, by splitting it with obj. For example, if the user entered 273 K, and thus temperature == '273 K', temperature.split(' ') creates a list by splitting it at every space, which becomes ['273', 'K']. However, those are still strings, so we want to convert the first to a float, so we do float(temperature.split(' ')[0]) # we only want to convert the first element.

Your improved code, by replacing temperature[:-1] with temp and replacing temperature[-1] with unit (it's always better programming practice to create variables rather than calculate the value multiple times):

def main():
    temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ")
    # algorithm for Farenheit to Kelvin, Celsius, and Rankine
    temp, unit = float(temperature.split(' ')[0]), temperature[-1] # multi-assignment
    if unit == "F":
        K = (temp + (459.67) * (5.0/9.0))
        print K
        C = (temp - 32) * (5.0/9.0)
        print C
        R = (temperature[:-1] + 459.67)
        print R
    # algorithm for Celsius to Kelvin, Fahrenheit, and Rankine
    elif unit == "C":
        K = (temp + 273.15)
        print K
        F = (temp * (9.0/5.0) + 32)
        print F
        R = (temp + 273.15) * (9.0/5.0)
        print R
    # algorithm for Kelvin to Celsius, Fahrenheit, and Rankine
    elif unit == "K":
        C = (temp - 273.15)
        print C
        F = (temp * (9.0/5.0) - 459.67)
        print F
        R = (temp * (9.0/5.0))
        print R
    # algorith for Rankine to Fahrenheit, Celsius, and Kelvin
    elif temperature[-1] == "R":
        F = (temp - 459.67)
        print F
        C = (temp - 491.67) * (5.0/9.0)
        print C
        K = (temp * (5.0/9.0))
        print K
main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.