Python Forum

Full Version: Importing a temperature converting module
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I'm trying to learn how to import functions from a module. This one converts degrees fahrenheit to celsius, or degrees celsius to fahrenheit, depending on user input. Here's the module's code:

#!/usr/bin/env python3
#TemperatureModule.py

def toCelsius(fahrenheit):
    """
    Accepts degrees Fahrenheit (fahrenhit argument)
    Returns degrees Celsius
    """
    celsius = (fahrenheit - 32) * 5/9
    return celsius

def toFahrenheit(celsius):
    """
    Accepts degrees Celsius (celsius argument)
    Returns degrees fahrenheit
    """
    fahrenheit = celsius * 9/5 + 32
    return fahrenheit
And here's the code of the .py file I'm trying to import the module into:
#!/usr/bin/env python3
#TemperatureModuleUser.py
import TemperatureModule as temp

def convertDegrees(F, C):
    getDegrees = float(input("Enter the number of degrees you wish to convert."))
    round(getDegrees,2)
    degreeUnits = ""
    While degreeUnits != "F" and degreeUnits != "C":
        
        degreeUnits = input("Are those degrees in Fahrenheit or Celsius?
                        + "(Enter F for Fahrenheit, or C for Celsius)")
        if(degreeUnits == F):
            F = temp.toCelsius(C)
            print(str(getDegrees) + " degrees Fahrenheit is equivalent to "
                  + str(F) + " degrees Celsius.")
        elif(degreeUnits == C):
            C = temp.toFahrenheit(F)
            print(str(getDegrees) + " degrees Celsius is equivalent to "
                  + str(C) + " degrees Fahrenheit.")
        else:
            print("Error. Enter F for Fahrenheit, or C for Celsius.")

convertDegrees(F, C)
Right now, the error is on line 9, saying that degreeUnits is invalid syntax. How can that be? I've declared that variable on line 8, right before the while loop statement.
It is because of While. it should be while.
Also, next time, please, post the entire traceback that you get. We need to see the whole thing. Do not just give us the last line. Take a time to read What to include in a post
Also you are missing a closing quote on line 11. Why are you concatenating that line?