HELP - Turning my program into one inside a class

I have to turn an old program into one that works as a class and only runs with a run() method. My variables don’t work, I’m not sure how to share my variables between the methods, and it runs automatically with or without the run().

class CSVReport:

def report_menu():

    '''This function creates a menu that prints out for the user to choose from.'''

    print("[1] Print yearly report by single month only")

    print("[2] Print yearly report")

    print("[3] Stop")

    print("")

report_menu()

report_selection = int(input("Choose: "))

def print_header():

    '''This creates a header that displays the month, day, year, and temperature.'''

    print("Year    Max    Min    Avrg")

    print("===== | ===  | ===  | ====")

def read_temp_file():

    """ This function will read the file that is imported on the weather temperatures."""

    header = True # Keeps track of reading the header

    temp_list = []

    year_data = {}

    with open('MIGRNDRA.csv') as temp_file: # opens the file and sets it

        for line in temp_file.readlines():

            if header:

                header = False

                continue

            if not line.strip():

                continue

            values = line.split(',') # creates a delimitter with the comma

            month = int(values[0].strip()) # these lines convert the records into the appropriate unit values

            day = int(values[1].strip())

            year = int(values[2].strip())

            temp = float(values[3].strip())

            if report_selection == 1: # this determines if the user wants a yearly report or by singly month only

                if str(month) != selected_month: # this compares the month variable with the user input; month is recognized as a string

                    continue

            else:

                pass

            if year not in year_data:

                year_data[year] = []

            year_data[year].append(temp)

    print_header() # prints the header out before each record

    for year, temps in year_data.items():

        min_temp = min(temps)

        max_temp = max(temps)

        average_temp = sum(temps) / len(temps)

        print("{:>5} | {:>3.1f} | {:.1f} | {:.1f}".format(year, max_temp, min_temp, average_temp)) # each record is printed out

        print("") # this is a line to create some space between each record

while report_selection != 0: # this will check each option available in the menu against the user's selection

    if report_selection == 1: # this option will allow them to select a month

        selected_month = input("Select month (1-12): ")

        print("")

        read_temp_file()

        pass

    elif report_selection == 2: # this prints the report as normal

        print("")

        read_temp_file()

        pass

    elif report_selection == 3: # this quits the program

        exit()

    else: # this will ask them to choose the correct option

        print("Choose a valid number.")

        print("")

    report_menu()

    report_selection = int(input("Choose: "))

def run():

    CSVReport.print_header()

    CSVReport.read_temp_file()

CSV_Report = CSVReport()

CSVReport.run()

Its somewhat unclear, since you are only showing what you describe as the contents of the class, so it is difficult to infer the structure. But it looks like you have a bunch of imperative code inside the class, rather than inside the class’s methods or run(), which is not going to do what you want. Also, you need to pass the magic first parameter self to the methods of the class, which you can then use to get the attributes of the current instance of the class (and thus share data between methods without explicitly passing it).

I highly suggest reading a basic Python tutorial about classes to deepen your understanding of this subject, and start with some much simpler examples and work up from there, rather than trying to port a longer existing script. That helps you learn step by step without getting lost, and allows you to quickly discover and correct mistakes. Best of luck!

https://docs.python.org/3/tutorial/classes.html