Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:用户输入和无效返回_Python_Python 3.x_User Interface - Fatal编程技术网

Python:用户输入和无效返回

Python:用户输入和无效返回,python,python-3.x,user-interface,Python,Python 3.x,User Interface,我试图用python创建一个程序,用户在其中输入温度和单位,让程序将温度转换为开尔文。对于代码,我需要帮助来检测输入无效的情况,如果输入无效,则返回None值 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

我试图用python创建一个程序,用户在其中输入温度和单位,让程序将温度转换为开尔文。对于代码,我需要帮助来检测输入无效的情况,如果输入无效,则返回None值

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
cas_number=“7732-18-5”
rho=1000
mu=1
Tm=273.15
Tb=373.13
k=0.58
单位=输入(“输入所需单位,'K'表示开尔文,'C'表示摄氏度,'F'表示华氏度\n”)
温度=输入(“输入温度值\n”)
def检查单位(单位):
如果单位='F'或单位='C'或单位='K':
返回真值
其他:
打印(“无效输入”)
返回错误
def检查温度(温度):
尝试:
浮子(温度)
返回真值
除:
返回错误
def将开度转换为开度(温度,单位):
如果check_temperature==True且check_units==True:
如果单位==“K”:
温度=温度
返回温度
elif单位==“C”:
温度+=Tm
返回温度
elif单位==“F”:
温度=(温度-32)/1.8+Tm
返回温度
其他:
打印(“无效输入”)
返回
def是_气体(温度):
如果检查\u温度并检查\u装置:
如果温度>=Tb:
返回真值
elif温度Tm且温度
测试检查单元()

你的检查温度()有问题。获得输入后,可以将其转换为float,如下所示:

temperature = input("Input temperature value \n")
temperature = float(temperature)


首先,您没有调用任何函数。程序所做的一切都是以用户输入单位和温度为基础的。你如何调用函数?如果你想调用
check\u units
,只需执行
variable\u name=check\u units(units)
,而
variable\u name
就是你想要的名称。
units = input("Input desired units, 'K' for Kelvin, 'C' for Celsius, and 'F' for Fahrenheit \n")

input_check = check_units(units) # calling the function

if input_check == True:
    print('Valid input')
if input_check == None:
    print('Invalid input')
temperature = input("Input temperature value \n")
temperature = float(temperature)
temperature = float(input("Input temperature value \n"))