Why can't I get this code to work?


#1
#==================================================================

# Program: Homework 3

# Programmer: 

# Date: January 26, 2018

# Abstract: This program uses functions to calculate pay, commission, gross pay, withholding, and net pay.

#==================================================================



def main():
    hourly_pay_rate=7.50
    commission_rate=0.05
    withholding_rate=0.25


def display_message():
    print("This program calculates the salesperson's pay.")
    print("Five values are displayed:")
    print("hourly pay, commission, gross pay, withholding, and net pay.")
    print("")
    
#Get name
    employee_name = input("Enter Employee's Name: ")

#Get Sales

    sales_amount = float(input("Enter the Sales Amount: "))

#Get Hours
    hours_worked = int(input("Enter Hours Worked by this Employee: "))

#Calculate Hourly Pay
    hourly_pay = hours_worked * hourly_pay_rate

#Calculate Commission
    commission = sales_amount * commission_rate

#Calculate Gross Pay
    gross_pay = hourly_pay_rate + commission

#Calculate Withholding
    withholding = gross_pay * withholding_rate

#Calculate Net Pay
    net_pay = gross_pay - withholding_rate
    display_results("hourly_pay, commission, gross_pay, withholding, net_pay")

    def display_results(hourly_pay, commission, gross_pay, withholding, net_pay):
        print ("The hourly pay amount is: %.2f" % hourly_pay_rate)
        print ("The commission amount is: %.2f" % commission_rate)
        print ("The gross pay amount is: %.2f" % gross_pay)
        print ("The withholding amount is: %.2f" % withholding_rate)
        print ("The net pay amount is: %.2f" % net_pay)

        #define global constants and call main
        hourly_pay_rate = float (7.50)
        commission_rate = float (0.05)
        withholding_rate = float (0.25)
#call the main function
main()

#2

from your main function, you never call the other functions, if i run your code here:

https://repl.it/@stetim94/InsubstantialUglyBlackpanther

it throws some errors. Also, keep in mind that hourly_pay_rate defined in main function has a local scope, so you can’t access in display_message, what you attempt here:

hourly_pay = hours_worked * hourly_pay_rate

hourly_pay_rate is undefined because of scope


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.