Skip to content

Instantly share code, notes, and snippets.

@ronsims2
Last active May 13, 2018 19:51
Show Gist options
  • Select an option

  • Save ronsims2/e7fa3ea997ec6141a20769a6a736b0b7 to your computer and use it in GitHub Desktop.

Select an option

Save ronsims2/e7fa3ea997ec6141a20769a6a736b0b7 to your computer and use it in GitHub Desktop.
Calculate pay the pythonic way!
# in algebra we learn y = m * x + b
# In python that would look like this
import math
def linear(m, x, b):
return m * x + b
# A function takes inputs, processes them and returns a a single item.
# This single item can be a numeric, a string, or something liek a dictionary or tuple
# in some advanced cases a function returns another function (we aint' goin' there)!
# What do these functiosn do?
def pythagorus (a, b):
c = a * a + b * b
return math.sqrt(c)
def find_angle (angle1, angle2):
if angle1 or angle2 > 179:
print('error')
elif angle1 or angle2 <= 0:
print('error')
elif angle1 + angle2 >= 180:
print('error')
else:
return 180 - angle1 -angle2
computepay = lambda h,r: (float(h) * float(r)) if float(h) < 40 else ((40 * float(r)) + ((float(h) - 40) * (float(r) * 1.5)))
hours = input('hours: ')
rate = input('rate: ')
print(computepay(hours, rate))
# Prompt for input
hours = input('hours: ')
rate = input('rate: ')
# h is hours r is rate
def computepay(h, r):
# Convert form string to numeric types, float for decimal like and in for
# whole numbers (this assumes hours will always be whole numbers)
# You can use float if hours will be decimal like
rt = float(r)
hr = int(h)
# create variables for overtime, regular time, and calculate dpay initialize as 0
# Since nothing has been allocated yet
regulartime = 0
overtime = 0
pay = 0
# Create condition that checks the only 2 cases you have
# There is either overtime or there isn't
if hr > 40: # Overtime
regulartime = 40
overtime = hr - 40
pay = (regulartime * rt) + (overtime * (rt * 1.5))
else: # Novertime
regulartime = hr
pay = regulartime * rt
# Return the pay calculated in the if/else
return pay
# Run the calculation and print he result using the hour and rates
# The function compute pay returns the pay, you need to save in a variable to output via print
workerpay = computepay(hours, rate)
print(workerpay)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment