Python Class 方法不返回导入的 class 方法?
Python Class Method not returning imported class method?
我有一个 class 接受用户选择(如下)
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()
第一部分似乎 运行 没有问题,因为从 class 导入的函数 SS.get_input(SS.display_input())
工作没有任何问题并且 return 为真或假,并且当他们这样做时 Newcastle.column_selection(Newcastle())
也能正常工作,因为它显示界面并接受用户输入。
所以,这一切似乎都奏效了。但是当用户选择 1 时,它应该 return CC.total_electricity()
方法,但它只是结束程序。
我也试过 return CC.total_electricity()
,但它也做同样的事情,但不起作用。知道为什么会这样吗?我整天都在研究它。
CC.total_electricity 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
并且已经过尝试和测试可以正常工作,只是当我导入它时它没有 return 任何东西并结束了程序。
您将用户输入与一个整数进行比较:
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: "))
我有一个 class 接受用户选择(如下)
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()
第一部分似乎 运行 没有问题,因为从 class 导入的函数 SS.get_input(SS.display_input())
工作没有任何问题并且 return 为真或假,并且当他们这样做时 Newcastle.column_selection(Newcastle())
也能正常工作,因为它显示界面并接受用户输入。
所以,这一切似乎都奏效了。但是当用户选择 1 时,它应该 return CC.total_electricity()
方法,但它只是结束程序。
我也试过 return CC.total_electricity()
,但它也做同样的事情,但不起作用。知道为什么会这样吗?我整天都在研究它。
CC.total_electricity 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
并且已经过尝试和测试可以正常工作,只是当我导入它时它没有 return 任何东西并结束了程序。
您将用户输入与一个整数进行比较:
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: "))