我将如何解决此代码块?

时间:2019-11-25 04:56:15

标签: python

如果您选择员工,则下面的代码应该输出以下内容

Do you wish to enter data for an (E)mployee, a (S)alesperson, or would you like to (Q)uit?: e
Enter the name of the employee: (employee name)
Enter (employee name)  hourly pay rate: (pay rate)
Enter (employee name) hours worked: (hours worked ```

Employee data: 
Employee name: (employee name)
Employee hourly pay: (hourly pay)
Hours worked: (hours worked)
Pay: (pay)

也就是说,如果您选择输入员工的数据^^^

如果您想为销售员选择以下数据,则为以下输出

Do you wish to enter data for an (E)mployee, a (S)alesperson, or would you like to (Q)uit?: s
Enter the name of the employee: (employee name)
Enter (employee name) hourly pay rate: (hourly pay)
Enter the number of hours worked by (employee name): (hours worked)
Enter the amount of sales made by (employee name): (sales made)
Enter the commission percentage that will be earned by (employee name): (commission percentage)

Salesperson data:
Employee name: (employee name)
Employee hourly pay: (hourly pay)
Hours worked: (hours worked)
Sales: (sales made)
Commission percentage: (commission percent)
Pay: (pay)

这是执行所有这些操作的整个代码块。请记住,这必须使用类和多态来完成。

class Employee:

    def __init__(self, hours_worked, hourly_rate):
        self.__hours_worked = 0
        self.__hourly_rate = 0

    def set_hours_worked(self, hours_worked):
        self.__hours_worked = hours_worked

    def get_hours_worked(self):
        return self.__hours_worked

    def set_hourly_rate(self, hourly_rate):
        self.__hourly_rate = hourly_rate

    def get_hourly_rate(self):
        return self.__hourly_rate

    def calc_pay(self):
        return self.__hourly_rate * self.__hours_worked

    def __str_(self):
        print()
        string1 = 'Employee data: ' + '\n' + 'Employee name: ' + employee_name + '\n' + 'Employee hourly pay rate: ' + employee_pay + '\n' + 'Hours worked: ' + employee_hours + '\n' + 'Pay: ' + employee_pay


class Salesman(Employee):

    def __init__(self, weekly_sales, commission, hours_worked, hourly_rate):
        Employee.__init__(self, hours_worked, hourly_rate)
        self.__weekly_sales = 0
        self.__commission = 0

    def set_weekly_sales(self, weekly_sales):
        self.__weekly_sales = weekly_sales

    def get_weekly_sales(self):
        return self.__weekly_sales

    def set_commission(self, commission):
        self.__commission = commission

    def get_commission(self):
        return self.__commission

    def calc_pay(self):
        return Employee.calc_pay(self) + (self.__weekly_sales * self.__commission)

    def __str__(self):
        print()
        string2 = 'Salesperson data: ' + '\n' + 'Employee name: ' + salesperson_name + '\n' + 'Employee hourly pay rate: ' + str(
            salesperson_pay) + '\n' + 'Hours worked: ' + salesperson_hours + '\n' + 'Sales: ' + salesperson_sales + '\n' + 'Commission percentage: ' + str(
            salesperson_commission) + '\n' + 'Pay: ' + str(salesperson_pay)


go_on = 'y'

while go_on == 'y':
    input1 = input('Do you wish to enter data for an (E)mployee, a (S)alesperson, or would you like to (Q)uit?: ')
    if input1 == 'e' or 'E':
        employee_name = input('Enter the name of the employee: ')
        employee_pay = input('Enter ' + employee_name + ' hourly pay rate: ')
        employee_hours = input('Enter the number of hours worked by ' + employee_name + ': ')
        employee_pay_rate = employee_pay * int(employee_hours)
        employee = Employee(employee_name, employee_pay, employee_hours, employee_pay_rate)
        print(employee)
    if input1 == 's' or 'S':
        salesperson_name = input('Enter the name of the employee: ')
        salesperson_pay = float(input('Enter ' + salesperson_name + ' hourly pay rate: '))
        salesperson_hours = input('Enter the number of hours worked by ' + salesperson_name + ': ')
        salesperson_sales = input('Enter the amount of sales made by ' + salesperson_name + ': ')
        salesperson_commission = float(input('Enter the commission percentage that will be earned by ' + salesperson_name + ': '))
        salesperson = Salesman(salesperson_pay, salesperson_hours, salesperson_sales, salesperson_commission)
        print(salesperson)
    else:
        go_on = 'n'

我确信我几乎所有的东西都对,但是我遇到了两个问题,我不知道如何解决。

1 个答案:

答案 0 :(得分:0)

错误:您正在while循环中向包含self的Employee传递5个参数。

  1. 从用户那里获取输入作为字符串时,请改用raw_inputinput
  2. input1 == "e" or "E"始终为true。做input1 == "e" or input1 == "E"

    通过这种方式,您可以利用while循环添加所需数量的用户。

还有一些其他要点:

  • 您有一个基类Employee,该基类的构造函数或init接受(self, hours_worked, hourly_rate)

推荐:(self, name, hours_worked, hourly_rate)

  • 在类Salesman的{​​{1}}方法中,尝试保持传递的参数顺序(在父类__init__参数首先出现的地方)。

推荐:`类业务员(员工):

Employees