0

Pythonで温度変換計算機を作ろうとしました。何が問題なのですか?を入力する20C20c=52f、 が正しくないことがわかっています。コードは次のとおりです。

def convert(changeto,temp):
    if changeto == "c":
        converted = (5/9)*(temp-32)
        print '%d C =  %d F' % (temp,converted)
    elif changeto == "f":
        converted = (9/5)*(temp+32)
        print '%d F = %d C' % (temp, converted)
    else:
        print "Error, type C or F for Celsius or Fahrenheit conversions."

print "Temperature Converter"

temp = float(raw_input("Enter a temperature: "))
changeto = str(raw_input("Convert to (F)ahrenheit or (C)elsius? "))

convert(changeto,temp)

raw_input("Any key to exit")
4

1 に答える 1

3

Python のバージョンによっては、5/9おそらくゼロに評価されます。に変更します5./9(ドットはを浮動小数点リテラル5に変換します)。

他の部門も同様です。

その上、あなたが持っている 2 つの数式は互いに逆ではないため、再検討する必要があります。

于 2013-11-01T21:28:13.070 に答える