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

2024-04-20 13:12:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个接受用户选择的类(如下)

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_Electrical class方法如下所示:

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

这已经被试过了,并且测试过了,当我导入它的时候,它不会返回任何东西,并且结束了程序


Tags: thekeyselftrueinputdefcolumnss
1条回答
网友
1楼 · 发布于 2024-04-20 13:12:57

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

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: "))

相关问题 更多 >