Home > Enterprise >  Function's return "looses" its value after exiting function
Function's return "looses" its value after exiting function

Time:07-03

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()

CodePudding user response:

i suggest you dont 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)

CodePudding user response:

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()

CodePudding user response:

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)

CodePudding user response:

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?

  • Related