1

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()
Manav Chhibber
  • 678
  • 3
  • 16
Yann
  • 25
  • 2
  • 1
    Please fix the indentation. – trincot Jul 02 '22 at 20:56
  • 2
    Your code makes a recursive call at the end, but it ignores what it returns. – trincot Jul 02 '22 at 20:57
  • 2
    it should return in else statement as well,`return inputUnitSelection()` – Manav Chhibber Jul 02 '22 at 20:58
  • *when it exits the function '(return) inputUnitSelection' has the value None*. When you write '(return)', are you thinking that Python implicitly returns the result of whatever the last line of the function is? Some other languages do that, but not Python. If you want to return something, you have to explicitly execute `return`, even if you're on the last line of the function. – slothrop Jul 02 '22 at 21:03

4 Answers4

3

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()
Yusuf Syam
  • 701
  • 1
  • 4
  • 18
2

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)
halfer
  • 19,824
  • 17
  • 99
  • 186
ilias-sp
  • 6,135
  • 4
  • 28
  • 41
  • Won't the "while True" loop block the programm indefinitely or will the function exits as soon as it has a return value ? – Yann Jul 03 '22 at 09:12
  • @Yann our function can be as stubborn as the user who provides wrong input, lets see who quits first: the function asking for a valid input, or the user providing wrong one :P – ilias-sp Jul 03 '22 at 09:29
  • The `return` statement exits immediately from the whole function, even if it occurs within a loop. – slothrop Jul 03 '22 at 12:30
1

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?

0
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)
Jakub
  • 174
  • 9
  • Dmit, i would be first, If stupid VS Code didnt need it to run this in terminal to be able have input...:D – Jakub Jul 02 '22 at 21:00
  • I didn't think a return would be necessary when recursively calling a function, thanks for your help – Yann Jul 03 '22 at 09:14
  • Np, sometimes paper and pencil is your friend , when you want to think about something. – Jakub Jul 03 '22 at 15:43