需要帮助在Python中简化我的脚本

时间:2016-11-06 05:26:40

标签: python

我是编程Python的新手,主要是通过“以艰难的方式学习python”来学习。我最近写了一个脚本,可以帮助我计算我的工作员工的加班收入。该脚本有效,但它看起来不对我。我很想知道如何为自己节省一些步骤。我知道使用全局变量是禁忌,但我无法找到另一种方法来获得这些函数之外的答案。谢谢!!

# THIS SCRIPT WILL CALCULATE THE NUMBER OF PAID HOURS, OVERTIME HOURS, PIECE RATE HOURS AND OVERTIME HOURS OWED TO AN EMPLOYEE.

number_of_straight_hours_worked = False
number_of_overtime_hours_worked = False
employee_travel_time = False
days_worked = False
employee_earnings = False
employee_hourly_rate = False
employee_overtime_rate = False

#Function to determine number of straight hours worked.
def straight_hours():
    global number_of_straight_hours_worked

    number_of_straight_hours_worked = float(number_of_straight_hours_worked)
    while number_of_straight_hours_worked == False:

        number_of_straight_hours_worked = raw_input("How many straight hours did the employee work?  ")

        try:
                int(number_of_straight_hours_worked)
        except ValueError:
            print("Must use a number")
            number_of_straight_hours_worked = False
        else:
            print ("Thank you.")
            #print number_of_straight_hours_worked

# Determines number of overtime hours.
def overtime_hours():
    global number_of_overtime_hours_worked

    number_of_overtime_hours_worked = float(number_of_overtime_hours_worked)
    while number_of_overtime_hours_worked == False:

        number_of_overtime_hours_worked = raw_input("How many overtime hours did the employee work?  ")

        try:
            int(number_of_overtime_hours_worked)
        except ValueError:
            print("Must use a number")
            number_of_overtime_hours_worked = False
        else:
            print ("Thank you.")
            #print number_of_overtime_hours_worked

#Calcualtes the employee's overtime rate.
def employee_ot_calculation():
    global employee_hourly_rate
    global employee_overtime_rate

    while employee_hourly_rate == False:
        employee_hourly_rate = raw_input("What is the employee's hourly rate?  ")

        try:
            float(employee_hourly_rate)
        except ValueError:
            print("Must use a number.")
            #print employee_hourly_rate
        else:
            employee_overtime_rate = float(employee_hourly_rate) * 1.5

#Stores travel time hours
def travel_time():
    global employee_travel_time

    while employee_travel_time == False:

        employee_travel_time = raw_input("How many hours of travel?  ")

        try:
            int(employee_travel_time)
        except ValueError:
            print("Must use a number.")
            employee_travel_time = False
        else:
            print ("Thank you.")
            #print employee_travel_time

#Stores number of working days. Not used in version .001
def number_of_days_worked():
    global days_worked

    while days_worked == False:

        days_worked = raw_input("How many days did the employee work?  ")
        days_worked = float(days_worked)
        try:
            int(days_worked)
        except ValueError:
            print("Must use a number.")
            days_worked = False
        else:
            print ("Thank you.")

#Stores earnings made by piece work from employee.
def employee_piece_earnings():
    global employee_earnings

    while employee_earnings == False:

        employee_earnings = raw_input("What did the employee earn through piece rate (format: 0.00)?  ")
        employee_earnings = float(employee_earnings)
        try:
            float(employee_earnings)
        except ValueError:
            print("Must use a number, no dollar sign.")
            employee_earnings = False
        else:
            print ("Thank you.")
            #print employee_earnings

# Calculates what the employee will earn this pay period between hours and piece work.
def overtime_piece():
    total_hours_worked = float(number_of_straight_hours_worked) + float(number_of_overtime_hours_worked)
    # print total_hours_worked

    actual_working_hours = float(total_hours_worked) - float(employee_travel_time)
    #print actual_working_hours

    piece_overtime = float(actual_working_hours) - 40
    #print piece_overtime

    overtime_rate = float(employee_earnings / actual_working_hours)
    #print overtime_rate

    earned_straight_pay = float(number_of_straight_hours_worked) * float(employee_hourly_rate)
    print "This employee earned $%.2f in straight pay: %.2f hours at $%.2f per hour" % (earned_straight_pay, number_of_straight_hours_worked, employee_hourly_rate)

    earned_hourly_overtime = (float(total_hours_worked) - float(actual_working_hours)) * float(employee_overtime_rate)
    print "This employee earned $%.2f in hourly overtime: %.2f hours at $%.2f per hour" % (earned_hourly_overtime, number_of_overtime_hours_worked, employee_overtime_rate)

    earned_piece_overtime = float(overtime_rate) * float(piece_overtime)
    print "This employee earned $%.2f in piece OT: %2f for each of working hour of the %.2f hours of overtime" % (earned_piece_overtime, overtime_rate, piece_overtime)

    total_employee_earnings = float(earned_straight_pay) + float(earned_hourly_overtime) + float(earned_piece_overtime) + float(employee_earnings)
    print "This employee earned a total of $%.2f this pay period." % total_employee_earnings

employee_ot_calculation()
straight_hours()
overtime_hours()
travel_time()
employee_piece_earnings()
overtime_piece()

1 个答案:

答案 0 :(得分:1)

为避免使用全局变量,您要做的是使用函数参数并返回值。

函数参数

让我们以您的第一个函数为例。 您可以在函数范围之外定义变量,并将其作为参数传递给两个括号。然后,您可以在函数内部方便地操作变量。您可以根据需要传递任意数量的参数。

number_of_straight_hours_worked = 1      # Define a variable outside of function
def straight_hours(number_of_straight_hours_worked):  #Pass as argument
    number_of_straight_hours_worked += 1 #Do whatever you want

返回值

函数straight_hours接受员工工作多少小时的输入。您要做的不是使用全局变量,而是返回值。

number_of_straight_hours_worked = ""      # Define a variable outside of function
def straight_hours(number_of_straight_hours_worked):  #Pass as argument
    number_of_straight_hours_worked = input("How many hours?")

    return number_of_straight_hours_worked

#Then retreive the value by doing
number_of_hours = straight_hours(number_of_straight_hours_worked)

如您所见,行number_of_hours = straight_hours()调用函数并影响返回number_of_hours变量的返回值。

其他说明

  • 我还建议缩小和简化变量名称。
  • 另外,当您在代码顶部声明一个变量时,我会建议将它们声明为NONE或根本不执行。将它们声明为False,其类型为布尔值然后将其转换为浮点数对我来说没有任何意义。
  • 根据您的实际计算确认您的验证码。这样,您可以单独测试您的计算。