Python类方法不返回导入的类方法?

问题描述 投票:0回答:1

我有一个用户选择的课程(下)

from MiniProject1.interfaces.s_selection import SecondarySelection as SS  # 
imports the secondary_selection function from selections
import MiniProject1.interfaces.newcastle_cm as IT
from MiniProject1.Newcastle.newcastle_cc import ColumnCalculation as CC

class Newcastle:

    def __init__(self):
        self.key_typed = input(str("Select what you want to do: "))

    @staticmethod
    def column_or_graph():
        if SS.get_input(SS.display_input()) is True:
            IT.column_manipulation()
            return True
        IT.graph_plotting()
        return False

    def column_selection(self):
        if self.key_typed == 1:
            CC.total_electricity()  # Calls the total_electricity method
        elif self.key_typed == 2:
            pass
        elif self.key_typed == 3:
            pass
        elif self.key_typed == 4:
            pass
        elif self.key_typed == 5:
            pass

def main():
    if Newcastle.column_or_graph() is True:
        Newcastle.column_selection(Newcastle())
    elif Newcastle.column_or_graph() is False:
        Newcastle.graph_plotting(Newcastle())


if __name__ == "__main__":
    main()

第一部分似乎没有问题,因为导入函数SS.get_input(SS.display_input())从类工作没有任何问题,并返回True或False,当他们做Newcastle.column_selection(Newcastle())工作时,因为它显示接口并接受用户输入。所以,一切似乎都有效。但是当用户选择1时,它应该返回CC.total_electricity()方法,但它只是结束程序。我也尝试了return CC.total_electricity(),但这也是做同样的事情,并且不起作用。有没有想过为什么会这样?我整天都在努力。

CC.total_electricity类方法如下所示:

import pandas as pd
class ColumnCalculation:
    """This houses the functions for all the column manipulation calculations"""

    @staticmethod
    def total_electricity():
        """Calculates the total amount of electricity used per year"""
        df = pd.read_csv("2011-onwards-city-elec-consumption.csv", thousands=',')
        df.set_index('Date', inplace=True)  # Sets index to months
        df.loc['Total'] = df.sum()  # Creates a new row for the totals of each year
        return print(df)  # Prints the dataframe

这已经尝试过,并经过测试可以正常工作,就在我导入它时,它不返回任何内容并结束程序。

python function class-method
1个回答
0
投票

您将用户输入与整数进行比较:

if self.key_typed == 1:

因此,您还需要将输入转换为整数。

所以与其:

self.key_typed = input(str("Select what you want to do: "))

做:

self.key_typed = int(input("Select what you want to do: "))
© www.soinside.com 2019 - 2024. All rights reserved.