1

To calculate a person's salary given their payrate and their hours worked.The code below computes to:

<function salary at 0x000002326B31D048>  

How do I make this run using the code below?

 payrate= input("How much do you get paid per hour")
    hrsworked= input("How many hours have you worked?")

    def salary(pay):
        if hrsworked<=40:
            salary=payrate*hrsworked
        elif hrsworked>40:
            salary=payrate*(1.5*(hrsworked-40))
        return salary
    print (salary)
Tanaka
  • 301
  • 2
  • 13

4 Answers4

3

You didn't call the salary function you defined, and you defined an argument you never use. Change the function definition to:

def salary(payrate, hrsworked):
    if hrsworked<=40:
        salary=payrate*hrsworked
    elif hrsworked>40:
        salary=payrate*(1.5*(hrsworked-40))
    return salary

and make your print call the function with appropriate arguments:

print(salary(payrate, hrsworked))

Mind you, the function doesn't need to take arguments in this case (since it can read the global variables you set), but it's best practice to do so; a function that can't be reused except by rewriting the globals for it isn't very flexible/reusable.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
2

You are trying to print a function by doing print(salary). Remove the pay argument from this function (it does nothing).

Finally, the print function you probably want is print(salary()) which will actually call the salary function and print it's value.

Shadow's answer is better since it doesn't rely on global variables, which easily become a problem in programs.

Saustin
  • 1,117
  • 6
  • 15
  • 27
2

Few things here.

  1. You are trying to print salary, which can either be the variable or the function. I would suggest renaming the function or the variable.
  2. I think you are trying to call salary in the print function. However, you have made pay a parameter which isn't being used in the print function. print(salary)
  3. You are working with numbers, so it would be best to convert the input functions to int's straight away, like this - payrate= int(input("How much do you get paid per hour")), do the same with the other one.

I fixed the code and it is usable now.

P.S. I'm not sure if we are supposed to give answers here, or just guide them through it. Anyways, I'll give it, please tell me if I'm not supposed to as I'm new here :)

https://hastebin.com/erajemejux.py

ADug
  • 324
  • 3
  • 10
1
payrate   = int(input("How much do you get paid per hour"))
hrsworked = int(input("How many hours have you worked?"))

    def salary():
        if hrsworked <= 40:
            salary = payrate * hrsworked
        elif hrsworked > 40:
            salary = payrate * (1.5 * (hrsworked - 40) )
        return salary

    print(salary())
  • You don't need pay, you don't use it.
  • Please, never forget to put spaces or enters, they make your code more clearly.
  • You write print(salary) instead of print(salary()). This isn't a good explanation but basically you was printing the "code name" of the function instead of executing it and making print the real result.
  • You are working with numbers, integers, not strings. You have to write int(input('Hi')) not input('Hi'), the second make a string, the first make a number.

Note:

I don't make this in my own codes but it's better do:

 def salary(payrate, hrsworked):

 # And

 print(salary(payrate, hrsworked))

Than:

 def salary():

Because you was using the global variables and in complexs code that could be problematic.

Another Note:

You have a function called salary and inside a variable called salary. That is bad, please change the name of one.

Community
  • 1
  • 1
Ender Look
  • 2,303
  • 2
  • 17
  • 41