函数Python中的函数

时间:2016-03-27 10:44:55

标签: python function

此功能可将摄氏度转换为华氏度

def celsius_to_fahrenheit (ctemp):
        temp_convert_to_fahr=int((ctemp+32)*1.8)

此功能将摄氏温度打印到华氏温度表

def print_celsius_to_fahrenheit_conversion_table(min,max):
    print("Celsius\tFahrenheit")
    print("------------------")
    for num  in range (min,max):
            tempc=num
            tempf= celsius_to_fahrenheit(tempc)
            print(tempc,"\t",tempf)

此功能从华氏温度转换为摄氏温度

def fahrenheit_to_celsius(tempf):
    f_to_c=int((tempf-32)/1.8)

此功能将华氏温度打印到摄氏温度表

def print_fahrenheit_to_celsius_conversion_table(min,max):
    print("Fahrenheit\tCelsius")
    print("------------------")
    for num in range (min,max):
            tempf=num
            tempc= fahrenheit_to_celsius(tempf)
            print(tempf,"\t",tempc)
print()
print_celsius_to_fahrenheit_conversion_table(0,11)
print()
print_fahrenheit_to_celsius_conversion_table(32,41)

每次运行时,我正在转换的列显示为“无”,有什么问题可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

您只是在函数中指定变量。你没有归还任何东西。只需将f_to_c=temp_convert_to_fahr=更改为return

即可
def celsius_to_fahrenheit (ctemp):
    return int((ctemp+32)*1.8)

def fahrenheit_to_celsius(tempf):
    return int((tempf-32)/1.8)

由于您没有明确地返回任何内容,因此函数会隐式返回None

答案 1 :(得分:0)

如果要从函数中获取值,则需要return显式函数的值,否则Python会自动执行return None

以下是更正的功能:

def celsius_to_fahrenheit (ctemp):
    temp_convert_to_fahr=int((ctemp+32)*1.8)
    return temp_convert_to_fahr

def fahrenheit_to_celsius(tempf):
    f_to_c=int((tempf-32)/1.8)
    return f_to_c

答案 2 :(得分:0)

缺少函数中的return语句

def celsius_to_fahrenheit (ctemp):
    temp_convert_to_fahr=int((ctemp+32)*1.8)
    return temp_convert_to_fahr

def fahrenheit_to_celsius(tempf):
        f_to_c=int((tempf-32)/1.8)
       return f_to_c

 def celsius_to_fahrenheit (ctemp):
        return int((ctemp+32)*1.8)  
def fahrenheit_to_celsius(tempf):
       return int((tempf-32)/1.8)