2016-09-16 72 views
0

所以我有4個不同的類,我需要將它們合併到一個名爲「Main」的程序的最後一個類中。然而,每當我運行Main程序時,它在從Employee類訪問emp_det字典到其他類時都會拋出一個屬性錯誤,以便我可以使用存儲在字典中的生成的Employee ID。 (還有兩個類叫做Weekly_PaidTimecard,但爲了簡潔起見,我只提到了3個類,因爲錯誤似乎是通用的。 (全部3類在不同的文件。)將字典從模塊導入到另一個模塊時的屬性錯誤

我不斷收到錯誤:

Traceback (most recent call last): 
    File "C:/Users/Saloni/Documents/Case Sudy 3/MAIN.py", line 28, in <module> 
s.MaintainTimecard() 
    File "C:/Users/Saloni/Documents/Case Sudy 3\Monthly_Paid.py", line 24, in MaintainTimecard 
if emp_id in Employee.emp_det: 
AttributeError: module 'Employee' has no attribute 'emp_det' 

Employee類:

# ATTRIBUTES: emp_nm, emp_ph, emp_add,emp_id, emp_dob 
from random import * 
class Employee: 
    emp_det={} #dictioary to save the details of the employees 
    #emp_id:[emp_nm,emp_ph,emp_add,emp_dob] 

    def add_emp(self): 
     lst=[] #to store all inputed details 
     print("Enter Employee Details:") 
     emp_nm=input("Name: ") 
     emp_ph=input("Contact Number: ") 
     emp_add=input("Address: ") 
     emp_dob=input("Date of Birth:") 
     lst.extend([emp_nm,emp_ph,emp_add,emp_dob]) #store the details 
     emp_id="emp"+str(randrange(1000,10000)) 
     while emp_id in Employee.emp_det: # avoid repetition 
      emp_id="emp"+str(randrange(1000,10000)) 
     Employee.emp_det[emp_id]=lst # make dictionary key and store in list 
     print("Your Employee ID is",emp_id) 

    def del_emp(self): 
     t=0 # to count number of invalid inputs 
     while t<=3: 
      emp_id=input("Employee ID:") 
      if emp_id in Employee.emp_det: 
       del Employee.emp_det[emp_id] 
       t=4 # to get the program out of the loop 
      else: 
       print("Invalid ID. Try Again.") 
       t+=1 

    def edit_emp(self): 
     t=0 # counting invalid inputs 
     while t<=3: 
      emp_id=input("Employee ID:") 
      if emp_id in Employee.emp_det: # checking validity 
       print("\n Edit: \n 1.Contact \n 2.Address \n") 
       ch=int(input("Option:")) 
       if ch==1: 
        Employee.emp_det[emp_id][1]=input("New Contact Number:") 
       elif ch==2: 
        Employee.emp_det[emp_id][2]=input("New Address:") 
       else: 
        print("Invalid Option") 
       t=4 #t o get the program out of the loop 
      else: 
       print("Invalid ID. Try Again.") 
       t+=1 

    def Edisplay(self): 
     print("The Employees are:") 
     print(" ID \t Name \t Contact \t Address \t Date of Birth") 
     for i in Employee.emp_det: # access to each dictionary element 
      print(i,"\t",end=" ") 
      for j in Employee.emp_det[i]: # access every value under the key 
       print(j,"\t",end=" ") 
      print("\n") 

月薪類

import Employee 
import Timecard 

class Monthly_Paid: 
    fixSalary = 40000 
    def AcceptTimeCard (self): 
     print ("Timecard details are:") 
     for i in Timecard.emp_tcinfo: 
      print(i, "\t", end ="") 
      for j in Timecard.emp_tcinfo[i]: 
       print(j,"\t",end=" ") 

    def Gen_Paycheck (self): 
     emp_id = input("please enter employee ID") 
     if emp_id in Employee.emp_det: 
      print ("Total Salary of " + emp_id + " is :" + fixSalary) 

    def MaintainTimecard (self): 
     emp_id = input("Please enter your employee ID") 
     if emp_id in Employee.emp_det: 
      print("\n 1.Edit Start Time Hour " 
        "\n 2.Edit Start Time Minute " 
        "\n 3. Edit End Time Hour " 
        "\n 4.Edit End Time Minute") 
      ch = int(input("Input option")) 
      if ch == 1: 
       Timecard.emp_tcinfo[emp_id][1] = input(
               "Input new Start Time Hour") 
      if ch ==2: 
       Timecard.emp_tcinfo[emp_id][2] = input(
               "Input new Start Time Minute") 
      if ch == 3: 
       Timecard.emp_tcinfo[emp_id][3] = input(
               "Input new End Time Hour") 
      if ch == 4: 
       Timecard.emp_tcinfo[emp_id][4] = input(
               "Input new End Time Minute") 
      else: 
       print("Invalid input") 

主要腳本

print ("Welcome to Employee Time Card System") 

import Employee 

e= Employee.Employee() 
e.add_emp() 

print("What kind of employee are you?") 
print ("\n 1.Monthly Paid \n 2.Weekly Paid") 

ch= int(input("Enter Choice")) 

if ch ==1: 
    import Monthly_Paid 
    import Timecard 
    s = Monthly_Paid.Monthly_Paid() 
    w = Timecard.Timecard() 
    print("Monthly Paid") 
    t1= "y" 
    while t1=="y" or t1=="Y": 
     print ("\n 1.See Time Card \n2.Edit TimeCard \n 3.See Paycheck") 
     ch1 = int(input("Enter Choice")) 
     if ch1 == 1: 
      s.AcceptTimeCard 
     if ch1 == 2: 
      s.MaintainTimecard() 
     if ch1 == 3: 
      s.Gen_Paycheck() 
     else: 
      print("Invalid Choice") 
      t1 = input("Continue with Monthly Paid? Y/N") 
elif ch == 2: 
    import Weekly_Paid 
    a= Weekly_Paid.Weekly_Paid() 
    t2= "y" 
    print ("Weekly Paid") 
    while t2=="y" or t2=="Y": 
     print ("\n 1.See Time Card \n2.Edit TimeCard \n 3.See Paycheck") 
     ch1 = int(input("Enter Choice")) 
     if ch1 == 1: 
      a.AcceptTimeCard() 
     if ch1 == 2: 
      a.MaintainTimeCard() 
     if ch1 == 3: 
      a.Gen_Paycheck() 
     else: 
      print("Invalid Choice") 
     t2 = input("Continue with Weekly Paid? Y/N") 
else: 
    print("Invalid choice") 
+0

這些代碼塊是否是同一個文件?如果不能將代碼示例與文件拆分爲相同的結構,請添加文件名,這在這裏很重要。 – shuttle87

+0

他們是不同的文件。對不起,我將這些文件編輯成3個不同的代碼塊。 –

+0

請包括確切的錯誤追溯,這是回答這個問題的重要信息。 – shuttle87

回答

0

import Employee將查找模塊Employee.py。如果沒有名爲Employee.py的文件,那麼您將無法使該導入工作。

所以在Monthly-Paid Class文件,你需要做的是這樣的:

from path.to.employee_file_name import Employee

的問題,那麼一個事實,即有一個名爲Employee模塊,但它包含了一個名爲Employee類出現。導入模塊Employee不會自動訪問該類。要訪問 Employee類的屬性emp_det,您必須指定類。所以,如果您導入使用

from Employee import Employee

要訪問您需要:

Employee.emp_det

或者,如果你輸入:

import Employee

然後訪問你需要:

Employee.Employee.emp_det

+0

非常感謝!它似乎工作。 –