Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jennyonjourney/e4982d3fedd6c70f1da239f86f1918b7 to your computer and use it in GitHub Desktop.
Save jennyonjourney/e4982d3fedd6c70f1da239f86f1918b7 to your computer and use it in GitHub Desktop.
Python for everybody - Assignment 4.6
def computepay(h,r):
if h<=40:
pay=h*r
elif h>40:
pay=40*r+(h-40)*r*1.5
return(pay)
hrs = input("Enter Hours:")
h = float(hrs)
rate = input("Enter rate:")
r = float(rate)
p = computepay(h,r)
print(p)
@silo3605
Copy link

silo3605 commented Jan 19, 2024

This worked for me perfectly, most of the times it has to do with indentations, or Colons or " " improperly placed.
def computepay(h, r):
if hours <= 40:
pay = hours * rate
else:
pay = 40 * rate + (hours - 40) * rate * 1.5
return pay

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

p = computepay(10, 20)
print("Pay", p)

Note: There are indentations in this code: if, else and returned must be aligned; pay, pay must also be aligned. If using python 3, hit the tab key once, which should place if right between f in def and space and c in computepay(h,r): The first pay should be right beneath hours of the if statement. The second pay should be beneath se of the else: and lastly return pay should be in alignment with else: so it should be if hours, else: and return pay aligned correctly and the same for pay by using the space bar in your pc. There two spaces after lines:6 and 10

@CodesbyRohit
Copy link

def computepay(h, r):
if h <= 40:
pay = h * r
elif h > 40:
pay = 40 * r + (h - 40) * r * 1.5
return pay # Corrected indentation

Get user input for hours and rate

hrs = input("Enter Hours: ")
h = float(hrs) # Convert input to float
rate = input("Enter Rate: ")
r = float(rate) # Convert input to float

Calculate pay

p = computepay(h, r)

Print the result

print(p)

Explanation of Changes
Indentation: The return pay statement is now correctly indented to be part of the computepay function.
Input Handling: The input handling remains the same, converting the input strings to floats for calculations.

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