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