-1
私はPythonでプログラムを作成しようとしています。ユーザーは温度を入力し、プログラムを持つユニットは温度をKelvinsに変換します。コードの場合、入力が無効である場合を検出し、入力が無効である場合にはNoneを返すようにする必要があります。Python:ユーザーの入力と無効戻り値
cas_number = "7732-18-5"
rho = 1000
mu = 1
Tm = 273.15
Tb = 373.13
k = 0.58
units = input("Input desired units, 'K' for Kelvin, 'C' for Celsius, and 'F' for Fahrenheit \n")
temperature = input("Input temperature value \n")
def check_units(units):
if units == 'F' or units == 'C' or units == 'K':
return True
else:
print("Invalid Input")
return False
def check_temperature(temperature):
try:
float(temperature)
return True
except:
return False
def convert_to_kelvin(temperature, units):
if check_temperature == True and check_units == True:
if units == 'K':
temperature = temperature
return temperature
elif units == 'C':
temperature += Tm
return temperature
elif units == 'F':
temperature = (temperature - 32)/1.8 + Tm
return temperature
else:
print("Invalid Input")
return
def is_gas(temperature):
if check_temperature and check_units:
if temperature >= Tb:
return True
elif temperature < Tb:
return False
else:
return None
def is_liquid(temperature):
if check_temperature and check_units:
if temperature > Tm and temperature < Tb:
return True
elif temperature <= Tm and temperature >= Tb:
return False
else:
return None
def is_solid(temperature):
if check_temperature and check_units:
if temperature <= Tm:
return True
elif temperature > Tm:
return False
else:
return None
、あなたは任意の関数を呼び出していません。書かれているように、すべてのプログラムは、ユーザー入力の単位と温度を持っています。 –
どのように関数を呼び出すのですか? –
'check_units'を呼びたいのであれば、単に' variable_name = check_units(units) 'を実行してください。' variable_name'はあなたの名前になりたいものです。 –