Skip to content

Instantly share code, notes, and snippets.

@ZeroRaven
Created March 29, 2019 16:50
Show Gist options
  • Save ZeroRaven/6c82f2c8e2a2dc327f42c9fe858da3c3 to your computer and use it in GitHub Desktop.
Save ZeroRaven/6c82f2c8e2a2dc327f42c9fe858da3c3 to your computer and use it in GitHub Desktop.
4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation…
def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p
hrs = input("Enter Hours:")
hr = float(hrs)
rphrs = input("Enter rate per hour:")
rphr = float(rphrs)
p = computepay(hr,rphr)
print(p)
@JoseLuis-Luna
Copy link

def computepay(h,r):

if h > 40:
    p= (40*r)+(h-40)*(1.5*r)
    
else:
    p= h*r
    

return p

hrs = input("Enter Hours:")
hr= float (hrs)

rate = input ("Enter Rate:")
rt= float(rate)

p = computepay(hr,rt)

print("Pay",p)

@iyashjayesh
Copy link

def computepay(hours,rate):
if hours>40.0:
p = 1.5rate(hours-40) +(40rate)
else:
p = rate
hours
return p

hours=input("Enter Hours:")
hour= float(hours)
rphrs = input("Enter rate per hours:")
rphr = float(rphrs)

p= computepay(hour,rphr)
print("Pay:",p)

@pushiko28
Copy link

1.5 * r * (h - 40) + (40 *r) from computation , I get different figures , could you please explain how come it gives the 498.75

is because you should use 0.5 (the midle with decimals) and not 1.5.

@jonbarba
Copy link

jonbarba commented Aug 14, 2020

#Many people are getting this wrong because they keep multiplying the overtime rate by 1.5 you need to multiply it by 0.5
def computepay(r):
if h > 40:
pays = (h - 40) * (0.5 * r) + (h * r)
return pays
elif h <= 40:
pays = h * r
return pays

hrs = input("Enter Hours: ")
h = float(hrs)
rate = input("Enter rate per hour: ")
r = float(rate)

print("Pay",computepay(r))

@NaelZaino
Copy link

I used this code and it worked:

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p

h = input("Enter Hours:")
h = float(h)
r = input("Enter rate per hour:")
r = float(r)

p=computepay(h,r)
print("Pay",p)

@NaelZaino
Copy link

you can also make it shorter by combining the float and input together like the following:

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p

h = float(input("Enter Hours:"))
r = float(input("Enter rate per hour:"))

p=computepay(h,r)
print("Pay",p)

@NaelZaino
Copy link

image

why its showing be like that? anybody can help me out plz

change print "pay" to capital P

@WardaLiaqat01
Copy link

#4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. Do not name your variable sum or use the sum() function.

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p

hrs = input("Enter Hours:")
hr = float(hrs)
rat = input("Enter Rate per hour:")
rph = float(rat)
p = computepay(hr,rph)
print("Pay",p)

@bhartimeena
Copy link

shows error in line 4??

what is an error ?

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p

hrs = input("Enter Hours:")
hr = float(hrs)
rphrs = input("Enter rate per hour:")
rphr = float(rphrs)

p = computepay(hr,rphr)
print(p)

@bhartimeena
Copy link

you should try this it's work 100%%%%%

@araujolaura
Copy link

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p

hrs = input("Enter Hours:")
hr = float(hrs)
rphrs = input("Enter rate per hour:")
rphr = float(rphrs)

p = computepay(hr,rphr)
print("Pay",p)

you're welcome!

@Nephy2911
Copy link

def computepay(h,r):
if h > 40:
p = 0.5 * r * (h-40) + (h * r)
else:
p = h * r
return p

hrs = input("Enter Hours: ")
hr = float(hrs)
rate = input("Enter Rate: ")
rte = float(rate)
p = computepay(hr,rte)

print("Pay",p)

@bhartimeena
Copy link

shows error in line 4??
Yeah same error I got but when I give a tab after if and else statement it's run perfectly.

@darshanghadi007
Copy link

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p

hrs = input("Enter Hours:")
hr = float(hrs)
rphrs = input("Enter rate per hour:")
rphr = float(rphrs)

p = computepay(hr,rphr)
print("Pay",p)

@Thrinesh15
Copy link

Guys can anyone send the correct code for the following question
Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours.
The output should be 498.75

@ironemin1k
Copy link

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p

hrs = input("Enter Hours:")
hr = float(hrs)
rphrs = input("Enter rate per hour:")
rphr = float(rphrs)

p = computepay(hr,rphr)
print("Pay", p)

#Guys! just add a space last line. don't forget this assigment very sensitive.

@Thrinesh15
Copy link

Thank you So much

@Harsha4747
Copy link

def computepay(h,r):
if h>40:
return
40r+(h-
40)r1.5
else:
return h
r
hrs=float(input("enter hours:")
r=float(input("enter rate per hour:")
p=computepay(hrs,r)
print("pay",p)

@Harsha4747
Copy link

Coming error

@Harsha4747
Copy link

Thank you So much
Bro coming error

@bhartimeena
Copy link

Thank you So much
Bro coming error

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p

hrs = input("Enter Hours:")
hr = float(hrs)
rphrs = input("Enter rate per hour:")
rphr = float(rphrs)

p = computepay(hr,rphr)
print("Pay",p)

you can try this!!

@Nephy2911
Copy link

Nephy2911 commented Oct 13, 2020 via email

@arjunrajapeta
Copy link

def computepay(hrs,rate):
if float(hrs) > 40:
pay = (40*float(rate)) + (float(hrs) -40) * float(rate) * 1.5
return pay
else:
pay = float(hrs)*float(rate)
return pay
hrs = input("Enter Hours:")
rate = input("Enter Rate:")
p = computepay(hrs,rate)
print("Pay" ,p)

@luissantaanna
Copy link

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h-40) + (40*r)
else:
p = h * r
return p
hrs = input("Enter Hours: ")
h = float(hrs)
rate = input("Enter Rate: ")
r = float(rate)

p = computepay(h,r)
print("Pay",p)

@Sohanaafroz955
Copy link

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p

hrs = input("Enter Hours:")
hr = float(hrs)
rphrs = input("Enter rate per hour:")
rphr = float(rphrs)

p = computepay(hr,rphr)
print("Pay",p)

@Laksara-Abeysinghe
Copy link

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p

hrs = input("Enter Hours:")
hr = float(hrs)
rphrs = input("Enter rate per hour:")
rphr = float(rphrs)

p = computepay(hr,rphr)
print("Pay",p)

this is right answer

@MR-Robot2001
Copy link

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p

hrs = input("Enter Hours:")
hr = float(hrs)
rphrs = input("Enter rate per hour:")
rphr = float(rphrs)

p = computepay(hr,rphr)
print("Pay",p)

//Perfect code

@Namita2411
Copy link

def computepay(h,r):
if h > 40:
p = 1.5 * r * (h - 40) + (40 *r)
else:
p = h * r
return p

hrs = input("Enter Hours:")
hr = float(hrs)
rphrs = input("Enter rate per hour:")
rphr = float(rphrs)

p = computepay(hr,rphr)
print("Pay", p)

100% right compiled and tested
![Screenshot 2020-06-21 at 9 27 34 PM](https://user-images.githubusercontent.com/67234874/85229185-172f4a00-b406-11ea-9c39-02a4d13c48ac

@satishsmse
Copy link

def computepay(h,r) :
if h > 40 :
pay = (1.5 * r * (h - 40) + (40 r))
else :
pay= h
r

return pay

sh = input ("Enter Hours: ")
sr = input ("Enter rate : ")
fh = float(sh)
fr = float(sr)
xp = computepay(fh, fr)
print("Pay",xp)

###Tested at https://www.py4e.com/ and 100% fine.

@mdsaq-hit
Copy link

def computepay(h,r):
if float(h) > 40:
p = 1.5 * float(r) * (float(h) - 40) + (40 *float(r))
else:
p = float(h) * float(r)
return float(p)

hrs = input("Enter Hours:")
hr = float(hrs)
rphrs = input("Enter rate per hour:")
rphr = float(rphrs)
p = computepay(hrs,rphr)
print("Pay",p)


this code will give you exact result without any error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment