学习Python,需要帮助创建一个具有用户给定值的对象,该对象将显示为列表

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

我正在完成今天到期的作业,但今天早上我早班了,我的大脑变得很粘稠。我在这里所做的是创建一个超类(Employee)和一个子类(ProductionWorkers)。

我认为我制作的类本身没问题,但程序最不需要的就是 ProductionWorker 类的对象,它允许用户为每个属性输入自己的数据。

最后,它需要显示用户给定的数据。我认为它需要是一个列表。

我现在尝试了一些不同的事情。您可以在代码底部附近看到列表(?)的一些剩余部分。这是我到目前为止所拥有的,如果有人可以提出解决方案,我将非常感激:

# This program is designed to create a superclass 'Employee' and
# a subclass 'ProductionWorkers'.

# Establish the main function.

def main():

    # Introduce the user to the program.
    print('Welcome to the employee information page.')
    print('Let\'s get your information entered into the system.')
     
    # Create the Employee superclass.
    class Employee:
        
        # Establish the attributes of Employee.
        def __init__(self, name, number):
            self.__name = name
            self.__number = number
         
         # The user needs to be able to set the values for these
         # attributes.
        def set_name(self, name):
            self.__name = name
                
        def set_number(self, number):
            self.__number = number
             
        # The program will need to return these values as well.
        def get_name(self):
            return self.__name
            
        def get_number(self):
            return self.__number
            
    # Establish the subclass ProductionWorkers.
    class ProductionWorkers(Employee):
       
       # We first need to initialize the preexisting attributes of
       # this subclass.
        def __init__(self, name, number):
           
            Employee.__init__(self, name, number)
           
           # Add the attributes unique to this subclass.
            self.shift_number = shift_number
           
            self.hourly_pay_rate = hourly_pay_rate
           
        # Allow the user to set the values of these new attributes.
        def set_shift_number(self, shift_number):
            self.__shift_number = shift_number
            
        def set_hourly_pay_rate(self, hourly_pay_rate):
            self.__hourly_pay_rate = hourly_pay_rate
            
        # Again, we want the program to display these values.
        def get_shift_number(self):
            return self.__shift_number
        
        def get_hourly_pay_rate(self):
            return self.__hourly_pay_rate
 
     # Now to make the objects that allow the user to enter their
     # own unique data.
    name = input(print('Enter your first name and last inital. '))
    number = input(print('Enter your employee number. '))
    shift_number = input(print('Do you work a day shift (1) or a night shift (2)? '))
    hourly_pay_rate = input(print('Enter your pay rate (per hour).'))
 
    employee_info = [name, number, shift_number, hourly_pay_rate]

    # Display the user's information.
    return str(ProductionWorkers)

# Call the main function.
main()

到最后我真的迷失了方向,这就是为什么它毫无意义。

python class hierarchy
1个回答
0
投票

我不知道你的错误是什么,但你不能在

print()
中包含
input()

此外,您正在调用
ProductionWorkers()
类,但您没有提供该类所需的参数。

这是我认为您想要实现的目标:

name = input('Enter your first name and last initial. ')
number = input('Enter your employee number. ')
shift_number = input('Do you work a day shift(1), or a night shift(2). ')
hourly_pay_rate = input('Enter your pay rate (per hour). ')

employee = ProductionWorkers(name, number, shift_number, hourly_pay_rate)
employee_info = [name, number, shift_number, hourly_pay_rate]

# I don't know why you have 'return str(ProductionWorkers)', dm me if you
# need any help (discord: j3sterishere)

print('Employee Information:')
print('Name:', employee.get_name())
print('Employee Number:', employee.get_number())
print('Shift Number:', employee.get_shift_number())
print('Hourly Pay Rate:', employee.get_hourly_pay_rate())
# or
for item in employee_info:
    print(item)

如果您需要任何其他帮助,请私信我(不和谐:j3sterishre)

© www.soinside.com 2019 - 2024. All rights reserved.