2

私は検索しましたが、この問題がどのように発生するのか、またはそれを解決する方法を理解できません。どんな助けでもありがたいです、ありがとう。

私のコードは次のとおりです。

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()

「50F」を入力した後のエラーメッセージ:

整数の後に単一のスペースを入力してから、華氏の場合はF、摂氏の場合はC、ケルビンの場合はK、ランキンの場合はRのいずれかを入力してください: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
>>> 
4

3 に答える 3

3

raw_inputstringユーザー入力のを返します。あなたが探しているのはfloatです。stringPythonでは、とfloatオブジェクトを一緒に追加する方法はありません。したがって、最初の入力をに変換して、float数学演算を実行できるようにする必要があります。

これを行う方法は次のとおりです。

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

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

したがって、このsplitメソッドは、スペース文字を使用してエントリを分割し、ユーザーが入力した内容からリストを作成します。temperatureスペースの前にある最初のものが含まれ、としてキャストされますfloatunits_of_option数字とスペースの後に入力される文字が含まれます。

したがって、ユーザーが次のように入力した場合:

123.5 F

変数の値は次のとおりです。

user_input = ["123.5", "F"]
temperature = 123.5
units_or_option = "F"
于 2013-03-26T04:06:36.890 に答える
2

必要な変更は2つだけです
。1つ目:直後に追加
し ます。例:temperature=float(temperature.split(' ')[0]),temperature.split(' ')[1]raw_inputtemperature

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


2番目:すべてを次
のように置き換えますtemperature[:-1]temperature[0]



最終的なコード

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()
于 2013-03-26T04:03:33.067 に答える
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文字列を返すので、文字列を分割するか、各ステップに変換してfloatにします。

数値を繰り返し計算するのではなく、変数として設定する方が常に良いので、次のことができます。

浮動小数点数への変換:

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])

で分割することにより、string.split(obj)の部分のリストを返します。たとえば、ユーザーがを入力した場合、したがって、はすべてのスペースで分割してリストを作成します。これは。になります。ただし、これらはまだ文字列であるため、最初のものをfloatに変換したいので、そうします。stringobj273 Ktemperature == '273 K'temperature.split(' ')['273', 'K']float(temperature.split(' ')[0]) # we only want to convert the first element

temperature[:-1]に置き換えてtemp置き換えることtemperature[-1]による、改善されたコードunit(値を複数回計算するよりも、変数を作成する方が常に優れたプログラミング手法です)。

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()
于 2013-03-26T04:02:59.643 に答える