Function's return "looses" its value after exiting function

Question:

I have written this code to make a temperature converter and challenged myself to only use functions but I don’t quite understand what is happening with the returned value.

The problem starts when I enter a wrong value like "celsiuss" or "klevin", it passes trough the else statement, asks me to enter a valid unit and makes me go to the beginning of the function as intended. I then write a valid answer, it goes trough the according if statement and returns the wanted value but as soon as it exits the function the return value becomes ‘None’.
I debugged it with breakpoints and inside the function ‘(return) inputUnitSelection’ has the value 1 (celsius in this case) but when it exits the function ‘(return) inputUnitSelection’ has the value None. I also checked it with a print. Why does it looses its value ?
Because of that my inputUnit variable has no value and the rest of the code does not work

What is making me even more confused is when I enter a valid answer the first time, the function returns a correct value and everything works fine. (Also checked it with breakpoints, debugger and print)

Here is the code :

Celsius = 1
Farenheit = 2
Kelvin = 3

def inputUnitSelection():
    unitInput = input(
        "What is the unit of the temperature you wish to convert ? (Celsius, Farenheit, Kelvin) n")
    
    if(unitInput == "Celsius" or unitInput == "celsius"):
        return Celsius 
    elif(unitInput == "Farenheit" or unitInput == "farenheit"):
        return Farenheit 
    elif(unitInput == "Kelvin" or unitInput == "kelvin"):
        return Kelvin 
    else:
        print("This is not an unit. Please enter a valid unit. n")
        inputUnitSelection()

inputUnit = inputUnitSelection()
Asked By: Yann

||

Answers:

I suggest you don’t recursively call the same function until user enters a legitimate option. instead use a loop and return value to the calling code only when the input option is valid. Code example below:

Celsius = 1
Farenheit = 2
Kelvin = 3

def inputUnitSelection():
    while True:
        unitInput = input("What is the unit of the temperature you wish to convert ? (Celsius, Farenheit, Kelvin) n")

        if(unitInput == "Celsius" or unitInput == "celsius"):
            return Celsius 
        elif(unitInput == "Farenheit" or unitInput == "farenheit"):
            return Farenheit 
        elif(unitInput == "Kelvin" or unitInput == "kelvin"):
            return Kelvin 
        else:
            print("This is not an unit. Please enter a valid unit. n")

inputUnit = inputUnitSelection()
print(inputUnit)
Answered By: ilias-sp

Your code indentation is pretty confusing but suggest the last line is not in function, u just have to give the inputUnitSelection() in your function a return statement

else:
    print("This is not an unit. Please enter a valid unit. n")
    return inputUnitSelection()
Answered By: zousan
Celsius = 1
Farenheit = 2
Kelvin = 3


def inputUnitSelection():


    unitInput = input(
        "What is the unit of the temperature you wish to convert ? (Celsius, Farenheit, Kelvin) n")

    if(unitInput == "Celsius" or unitInput == "celsius"):
        return Celsius
    elif(unitInput == "Farenheit" or unitInput == "farenheit"):
        return Farenheit
    elif(unitInput == "Kelvin" or unitInput == "kelvin"):
        return Kelvin
    else:
        print("This is not an unit. Please enter a valid unit. n")
        return inputUnitSelection()

inputUnit = inputUnitSelection()
print(inputUnit)
Answered By: Jakub

In addition to the answers that explain your problem, you may also want research Enum and use title() to write your code like this:

from enum import Enum

class ConversionType(Enum):
    Celsius = 1
    Farenheit = 2
    Kelvin = 3


def inputUnitSelection():
    while True:
        unitInput = input("Celsius, Farenheit, Kelvin? n")
        if(unitInput.title() == ConversionType.Celsius.name):
            return  ConversionType.Celsius.value
        elif(unitInput.title() == ConversionType.Farenheit.name):
            return ConversionType.Farenheit.value 
        elif(unitInput.title() == ConversionType.Kelvin.name):
            return ConversionType.Kelvin.value  
        else:
            print("This is not an unit. Please enter a valid unit. n")

inputUnit = inputUnitSelection()
print(inputUnit)

Reading:

What are enums and why are they useful?

Answered By: user19242326
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.