2011-12-17 4 views
0

저는 Python 3의 클래스로 작업 중이며, 그들과 힘든 시간을 보내고 있습니다. 나는 여기에 두 가지 프로그램을 가지고있다. (하나는 다른 하나에 임포트 됨) 아이디어는 당신이 종업원 명단을 만들고 있고 종업원 인 Hourly, Salary, Volunteer의 3 가지 클래스가 있다는 것이다.클래스로 작업하는 Python

각 클래스의 show_pay에 문제가있는 것 같습니다. 또한 샐러리 클래스에서 문자열을 정수로 나눌 것을 시도하고 있지만 코드를 작성한 방법으로는 주위를 둘러 볼 방법이 확실치 않다는 것을 알고 있습니다. 시간별 수업이 목록에 인쇄되지 않는 것 같습니다.

미리 감사드립니다. 나는 이것과 정말로 혼동스럽고 나는이 프로젝트를 끝내려고 노력하고있다.

첫 번째 프로젝트 (직원이)

#set global constant 
    SHIFT_2 = 0.05 
    SHIFT_3 = 0.10 

    #person class 
    class Person: 
     #initialize name, ID number, city 
     def __init__(self, name, ID, city): 
      self.__ID = ID 
      self.__name = name 
      self.__city = city 
     #display employee name 
     def show_person(self): 
      print('Name:', self.__name) 
      print('ID:', self.__ID) 
      print('City:', self.__city) 
     #display salary 
     def show_pay(self): 
      print('I make lots of money') 


     #return formatting  
     def __str__(self): 
      name_string = '(My name is ' + self.__name +')' 
      return name_string 

    # Hourly employee class 
    class Hourly(Person): 
     #initialize method calls superclass 
     def __init__(self, name, ID, city, base_pay, shift): 
      Person.__init__(self, name, ID, city) 
      self.__base_pay = base_pay 
      self.__shift = shift 

     #show_pay overrides the superclass and displays hourly pay rates 
     def show_pay(self): 
      if self.__shift == 1: 
       print('My salary is ', self.__base_pay) 
      elif self.__shift == 2: 
       print('My salary is ', (self.__base_pay * SHIFT_2) + self.__base_pay) 
      elif self.__shift == 3: 
       print('My salary is ', (self.__base_pay * SHIFT_3) + self.__base_pay) 

    #salary employee class 
    class Salary(Person): 
      #intialize method calls superclass 
     def __init__(self, name, ID, city, ann_salary): 
      Person.__init__(self, name, ID, city) 
      self.__salary = ann_salary 


     #show pay overrides superclass and displays salary pay rates 
     def show_pay(self): 
      print('I make ', self.__salary) 
      print('which is ', self.__salary // 26, 'every two weeks.') 


    #volunteer employee class 
    class Volunteer(Person): 
     def __init__(self, name, ID, city): 
      Person.__init__(self, name, ID, city) 

     def show_pay(self): 
       print('I am a volunteer so I am not paid.') 

이 메인 프로그램

import employee 

    def main(): 
     #create list 
     employees = make_list() 

     #display list 
     print('Here are the employees.') 
     print('-----------------------') 

     display_list(employees) 

    def make_list(): 
     #create list 
     employee_list = [] 

     #get number of hourly employees 
     number_of_hourly = int(input('\nHow many hourly will be entered? ')) 
     for hourly in range(number_of_hourly): 
      #get input 
      name, ID, city = get_input() 

      base_pay = input('Enter employee base pay: ') 

      shift = input('Enter employee shift 1,2, or 3: ') 
      #create object 
      Hourly = employee.Hourly(name, ID, city, base_pay, shift) 
      #add object to list 
      employee_list.append(Hourly) 

     #get number of salary employees 
     number_of_salary = int(input('\nHow many salary will be entered? ')) 
     for salary in range(number_of_salary):  
      #get input 
      name, ID, city = get_input() 

      ann_salary = input('Enter employee annual salary: ') 
      #create object 
      salary = employee.Salary(name, ID, city, ann_salary) 
      #add object to list 
      employee_list.append(salary) 

     #get volunteers 
     number_of_volunteers = int(input('\nHow many other volunteers will be entered? ')) 
     for volunteers in range(number_of_volunteers): 
      #get info 
      name, ID, city = get_input()  
      #create object 
      volunteer = employee.Person(name, ID, city) 
      #add object to list 
      employee_list.append(volunteer) 


     #invalid object 
     employee_list.append('\nThis is invalid') 
     #return employee_list 
     return employee_list 

    def get_input(): 
     #input name 
     name = input("Employee's name: ") 
     #validate 
     while name == '': 
      print('\n Name is required. Try again.') 
      name = input("Employee's name: ") 

     ID_valid = False 

     ID = input("Employee's ID: ") 

     while ID_valid == False: 

      try: 
       ID = float(ID) 
       if ID > 0: 
        ID_valid = True 
       else: 
        print("\nID must be > 0. Try again.") 
        ID = input("Employee's age: ") 
      except ValueError: 
       print("\nID must be numeric. Try again.") 
       ID = input("Employee's ID: ") 

     #get city 
     city = input("Enter employee's city of residence: ") 

     #return values 
     return name, ID, city 



    def display_list(human_list): 
     #create for loop for isinstance 
     for human in human_list: 
      #create isinstance 
      if isinstance(human, employee.Person): 

       print(human) 

       human.show_person() 

       human.show_pay() 
       print 
      else: 
       print('Invalid employee object') 

    #call main function 
    main() 
+3

귀하의 질문에 대한 정확한 답변은 무엇입니까? 또한 모든 클래스 변수 앞에'__'을 사용할 필요가 없습니다. 가독성을 실현할 수 있습니다. – Blender

+0

클래스와 함께 show_pay가 특정 클래스 (Hourly, Salary 또는 Volunteer) 아래에 무엇이든지 변경되어야하지만 어떤 이유로 그것이 작동하지 않는다고 생각합니다. __에 대해 죄송합니다. 나는이 책을 처음 접했을뿐 나의 책은 그렇게하고있다. 나는 다른 어떤 방법을 확신하지 못한다. –

+0

사실 - 코딩 위의 코드가 작동하지 않는다면, 접두사가 붙은'__'이 그 코드와 관련이 있습니다. "어쩌면 이것이 파이썬에서 사적 속성을하는 법"이라는 것을 보았을 것입니다 - 그것은 정확하지 않을 것입니다. 그러나 이것은 오작동과 같은 코드를 오히려 쉽게 만들 수 있습니다. 그냥 그들을 제거하십시오. – jsbueno

답변

1

당신은 문자열로 급여를 입력하고에게 있습니다. 그것을 변환하십시오.

ann_salary = int(input('Enter employee annual salary: ')) 
관련 문제