Python Forum
Importing a temperature converting module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing a temperature converting module
#1
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.
Reply
#2
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
Reply
#3
Also you are missing a closing quote on line 11. Why are you concatenating that line?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  importing variables from module 8376459 1 245 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  no module named 'docx' when importing docx MaartenRo 1 709 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  Help gathering Temperature from API response road102 5 1,019 Dec-16-2022, 08:30 PM
Last Post: road102
  My code displays too much output when importing class from a module lil_e 4 1,102 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  Importing module in jupyter Noteboook ajitnayak1987 0 1,725 Jun-04-2021, 12:26 PM
Last Post: ajitnayak1987
  get data (temperature and humidity) from DHT22 into CSV and sending them over the net apollo 0 3,797 Apr-16-2021, 07:49 PM
Last Post: apollo
  monitoring the temperature of the CPU with Python apollo 2 8,523 Apr-13-2021, 05:39 PM
Last Post: apollo
  ERROR: importing desired module mbgamer28 0 1,648 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  print CPU temperature samuelbachorik 12 5,922 Aug-04-2020, 03:28 PM
Last Post: Axel_Erfurt
  importing module - not working jdhamblett 3 2,945 Jun-22-2020, 07:33 PM
Last Post: jdhamblett

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020