Mikey Durocher
Mikey Durocher

Reputation: 23

Function within Function Python

This function converts celsius to fahrenheit

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

This function prints the celsius to fahrenheit table

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)

This function converts from fahrenheit to celsius

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

This function prints the fahrenheit to celsius table

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)

Every time I run this, my column that is being converted shows up as "none", any help as to what is wrong?

Upvotes: 0

Views: 124

Answers (3)

Ankanna
Ankanna

Reputation: 777

missing return statements in the functions

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

or

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

Upvotes: 0

Rob Murray
Rob Murray

Reputation: 1903

When you want to get the value from a function you need to return the value from the function explicitly, otherwise Python automatically does return None.

Here's the corrected functions:

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

Upvotes: 0

zondo
zondo

Reputation: 20336

You are just assigning variables in your functions. You aren't returning anything. Just change f_to_c= and temp_convert_to_fahr= to return:

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

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

Since you don't return anything explicitly, the functions return None implicitly.

Upvotes: 2

Related Questions