How to Define Parameters Outside of a Function Loop?

Viewed 304

I am trying to execute a function that will repeat if given the right input as seen in my while statement. However my program returns the same value in additional loops without option to use new hrs and rate inputs if my input statements are above my function. If they are within the function as currently presented, it states my (hrs,rate) is not defined. How would I define the hrs and rate parameters while keeping my inputs within the function?

def CalPay(hrs,rate):
    hrs = input('Please enter number of hours worked for this week:')
    rate = input('What is hourly rate:')
    try:
        hrs = float(hrs)
    except:
        hrs = -1
    try:
        rate = float(rate)
    except:
        rate = -1
    if hrs <= 0 :
        print('You have entered wrong information for hours.')
    elif rate <= 0 :
        print('You have entered wrong rate information.')
    elif hrs <=  40 :
        pay = hrs * rate
        print ('Your pay for this week is:', pay)
    elif hrs > 40 and hrs < 60 :
        pay = ((hrs - 40) * (rate * 1.5)) + (40 * rate)
        print ('Your pay for this week is:', pay)
    elif hrs >= 60 :
        pay = ((hrs - 60) * (rate * 2.0)) + (20 * (rate * 1.5)) + (40 * rate)
        print ('Your pay for this week is:', pay)

while True:
    CalPay(hrs,rate)
    yn = input('Do you wish to repeat this program? (y/n)').lower()
    if yn == 'y' :
        continue 
    if yn == 'n' :
        break
print ('Done!')
2 Answers

In your while loop you call CalPay and pass in hrs and rate. You are calling a function and giving it two values that you create inside of the function i.e. they don't exist when you call CalPay, so you get an error. Just collect the inputs in your while loop instead of inside your function. Like this:

while True:
    hrs = input('Please enter number of hours worked for this week:')
    rate = input('What is hourly rate:')
    CalPay(hrs,rate)
    yn = input('Do you wish to repeat this program? (y/n)').lower()
    if yn == 'y' :
        continue 
    if yn == 'n' :
        break
print ('Done!')

Note: You'll have to adjust the logic for repeating the program accordingly.

Another even better solution would be to remove the arguments from CalPay and the function call and then collect the information you need inside the function. As mentioned by Anurag.

def CalPay():
    hrs = input('Please enter number of hours worked for this week:')
    rate = input('What is hourly rate:')
    try:
        hrs = float(hrs)
    except:
     .
     .
     .

while True:
    CalPay()
    yn = input('Do you wish to repeat this program? (y/n)').lower()
    if yn == 'y' :
        continue 
    if yn == 'n' :
        break
print ('Done!')

First, you need to initialize the two variables after the function, so whenever the time you enter the variables' values, they've to be defined.

Something like that:

# those two lines will be after the function
hrs = 0   
rate = 0

Full program will go like that: -

def CalPay(hrs,rate):
    hrs = input('Please enter number of hours worked for this week:')
    rate = input('What is hourly rate:')
    try:
        hrs = float(hrs)
    except:
        hrs = -1
    try:
        rate = float(rate)
    except:
        rate = -1
    if hrs <= 0 :
        print('You have entered wrong information for hours.')
    elif rate <= 0 :
        print('You have entered wrong rate information.')
    elif hrs <=  40 :
        pay = hrs * rate
        print ('Your pay for this week is:', pay)
    elif hrs > 40 and hrs < 60 :
        pay = ((hrs - 40) * (rate * 1.5)) + (40 * rate)
        print ('Your pay for this week is:', pay)
    elif hrs >= 60 :
        pay = ((hrs - 60) * (rate * 2.0)) + (20 * (rate * 1.5)) + (40 * rate)
        print ('Your pay for this week is:', pay)


hrs = 0
rate = 0

while True:
    CalPay(hrs,rate)
    yn = input('Do you wish to repeat this program? (y/n)').lower()
    if yn == 'y' :
        continue 
    if yn == 'n' :
        break
print ('Done!')

OUTPUT

Please enter number of hours worked for this week: 36
What is hourly rate: 6
Your pay for this week is: 216.0
Do you wish to repeat this program? (y/n)Y

Please enter number of hours worked for this week: 12
What is hourly rate: 5
Your pay for this week is: 60.0
Do you wish to repeat this program? (y/n)

Related