Create and run a function in Python


#Return the post-tax earnings of an individual,

#assuming varying tax brackets depending on annual wage

def calcEarnings(hours, wage):

earnings = hours*wage

if earnings >= 2000:

#30% tax rate for weekly earnings above $2000

earnings = earnings*.70

else:

#15% tax rate for weekly earnings below $2000

earnings = earnings*.85

return earnings

#return the raw earnings of an individual (assuming no varying rates for overtime)

def calcEarnings_pretax(hours, wage):

earnings = hours*wage

return earnings

def main():

hours = float(input('Enter hours worked for the week: '))

wage = float(input('Enter dollars paid per hour: '))

total = calcEarnings(hours, wage)

total_pre_tax = calcEarnings_pretax(hours, wage)

taxes = total_pre_tax - total

print('Pre-tax earnings for {hours} hours at ${wage:.2f} per hour are ${total_pre_tax:.2f}.'

.format(**locals()))

print('Post-tax earnings for {hours} hours at ${wage:.2f} per hour are ${total:.2f}.'

.format(**locals()))

print('You gave uncle sam ${taxes:.2f} this week!'

.format(**locals()))

main()


Output:

Enter hours worked for the week: 40
Enter dollars paid per hour: 55
Pre-tax earnings for 40.0 hours at $55.00 per hour are $2200.00.
Post-tax earnings for 40.0 hours at $55.00 per hour are $1540.00.
You gave uncle sam $660.00 this week!


You can run the above yourself if you copy/paste it into a notebook or text editor. Will ask you for two inputs (hours, wage) and calculate pre-tax earnings, post-tax earnings, and taxes paid.






Ace your next data science interview

Get better at data science interviews by solving a few questions per week



Find a bug? Submit a suggested change on Github, or message me on Twitter.