首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >修改Python代码以格式化程序输出

修改Python代码以格式化程序输出
EN

Stack Overflow用户
提问于 2022-06-15 15:56:04
回答 2查看 56关注 0票数 0

我的当前Python代码返回以下内容:

代码语言:javascript
运行
复制
Employee Details:
    Employee Name: John Smith, Department: Accounting, Weekly Pay: $2511.25
    Employee Name: Mary Smith, Department: Finance, Weekly Pay: $2500
    Employee Name: Justin White, Department: Marketing, Weekly Pay: $2000.0

Total Weekly Pay: $7011.25

我想把这个还回去:

代码语言:javascript
运行
复制
Employee Type      Employee Name  Department     Weekly Pay
-------------      -------------  ----------     ----------
Hourly Paid        John Smith     Accounting     $   1281.25
Salary Paid        Mary Smith     Finance        $   2500.00
Commission Paid    Justin White   Marketing      $   2000.00

Total Weekly Pay:$5781.25

我该怎么修改密码?Python附在下面:

主要职能:

代码语言:javascript
运行
复制
import employee
def total_pay(employees):
    '''Take employee list as parameter
    Return total paid amount'''
    total=0.0
    for x in employees:
        total+=x.pay()
    return total

def print_employee_list(employees):
    '''Display all employee details'''
    for x in employees:
        print('\t',x)

if __name__ == "__main__":
    '''Main method Create each subclass object'''
    commissionpaid=employee.CommissionPaid(500,50000)
    commissionpaid.set_name('Justin White')
    commissionpaid.set_department('Marketing')
    hourlypaid=employee.HourlyPaid(20.5,55)
    hourlypaid.set_name('John Smith')
    hourlypaid.set_department('Accounting')
    salarypaid=employee.SalaryPaid(2500)
    salarypaid.set_name('Mary Smith')
    salarypaid.set_department('Finance')
    employees=[hourlypaid,salarypaid,commissionpaid]
    #display employee details
    print('Employee Details:')
    print_employee_list(employees)
    #Total salary
    print('\nTotal Weekly Pay: $',total_pay(employees))
Modules:

#create a super class Employee
class Employee:
    def __init__(self,name="",department=""):
        ''' Constructor assign values to attributes'''
        self.__name=name
        self.__department=department
    def get_name(self):
        '''Return name of the employee'''
        return self.__name
    def get_department(self):
        '''Return department of the employee'''
        return self.__department
    def set_name(self,name):
        '''Set employee name'''
        self.__name=name
    def set_department(self,department):
        '''set department where employee working'''
        self.__department=department
    def pay(self):
        '''Return salary of employee'''
        return 0.0
    def __str__(self):
        '''Return employee details'''
        return "Employee Name: "+self.__name+", Department: "+self.__department

#Create subclass CommissionPaid
class CommissionPaid(Employee):
    def __init__(self,base_rate=0.0,sales=0.0):
        '''Initialize attributes'''
        super().__init__()
        self.__base_rate=base_rate
        self.__sales=sales
    def get_base_rate(self):
        '''Return base rate of he employee'''
        return self.__base_rate
    def get_sales(self):
        '''Return sales amount'''
        return self.__sales
    def set_base_rate(self,base_rate):
        '''set base rate of the employee pay'''
        self.__base_rate=get_base_rate
    def set_sales(self,sales):
        '''set sales amount of the employee'''
        self.__sales=sales
    def pay(self):
        '''Calculate and return payment of commission based employee'''
        commission=0.0
        if self.__sales>30000:
            commission=self.__sales*.03
        elif self.__sales>=5000 and self.__sales<=30000:
            commission=self.__sales*.01
        return self.__base_rate+commission
    def __str__(self):
        return super().__str__()+", Weekly Pay: $"+str(self.pay())

#Create subclass HourlyPaid
class HourlyPaid(Employee):
    def __init__(self,hourly_rate=0.0,hours=0.0):
        '''Set attributes of the class'''
        super().__init__()
        self.__hourly_rate=hourly_rate
        self.__hours=hours
    def get_hourly_rate(self):
        '''Return hourly payment rate of the employee'''
        return self.__hourly_rate
    def get_hours(self):
        '''Return hours the employee worked'''
        return self.__hours
    def set_hourly_rate(self,hourly_rate):
        '''Set hourly rate of the employee'''
        self.__hourly_rate=hourly_rate
    def set_hours(self,hours):
        '''Set hours employee worked'''
        self.__hours==hours
    def pay(self):
        '''calculate payment'''
        if self.__hours<=40:
            return self.__hourly_rate*self.__hours
        else:
            return (self.__hourly_rate*40)+(self.__hourly_rate*(self.__hours)*1.5)
    def __str__(self):
        return super().__str__()+", Weekly Pay: $"+str(self.pay())
#Create subclass SalryPaid
class SalaryPaid(Employee):
    def __init__(self,salary):
        '''Set attributes'''
        super().__init__()
        self.__salary=salary
    def set_salary(self,salary):
        '''set salry'''
        self.__salary=salary
    def get_salary(self):
        '''Get salary'''
        return self.__salary
    def pay(self):
        '''calculate pay amount'''
        return self.__salary
    def __str__(self):
        return super().__str__()+", Weekly Pay: $"+str(self.pay())
EN

回答 2

Stack Overflow用户

发布于 2022-06-15 16:04:47

您应该将数据和表示解耦。删除所有这些打印,只需对类中的数据进行建模。然后创建一个脚本,该脚本接受所有这些实例,并按需要格式化它。

这会容易得多,但如果在做完之后你仍然不知道如何做,再次发布新的代码。

票数 2
EN

Stack Overflow用户

发布于 2022-06-15 16:25:31

这看起来不像你想要的那样..。但它应该会让你想到..。这种方法是非常有限的,因为列宽是固定的,任何较大的都会被切断。但是,要动态地调整列的大小,需要做更多的工作。

代码语言:javascript
运行
复制
# Here, instead of duplicating all your code, I'll just dummy up some data to show you how the printing works...
from decimal import Decimal as D

employees = [
    { 'type': 'Hourly Paid',
      'name': 'John Smith',
      'dept': 'Accounting',
      'pay_wk': D('1281.25')
    },{
      'type': 'Salary Paid',
      'name': 'Mary Smith',
      'dept': 'Finance',
      'pay_wk': D('2500.00')
    }
]

fmt = "%-18.18s %-18.18s %-18.18s %s%17.17s"
underline = '--------------------'

print(fmt % ('Employee Type', 'Employee Name', 'Department', ' ', 'Weekly Pay'))
print(fmt % (underline, underline, underline, '-', underline))

for emp in employees:
    print(fmt % (emp['type'], emp['name'], emp['dept'], '$', emp['pay_wk']))

输出:

代码语言:javascript
运行
复制
Employee Type      Employee Name      Department                 Weekly Pay
------------------ ------------------ ------------------ ------------------
Hourly Paid        John Smith         Accounting         $          1281.25
Salary Paid        Mary Smith         Finance            $          2500.00
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72634412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档