Trying to find the total time worked in this. Cant seem to figure out what I have wrong.
#Problem 3, Python, Extra Credit
hourStart = int(input("Please enter the hour that Jimmy started work."))
minStart = int(input("Please enter the minute on the hour that Jimmy started work."))
hourEnd = int(input("Please enter the hour that Jimmy ended work."))
minEnd = int(input("Please enter the minute on the hour that Jimmy ended work."))
lunchHourStart = int(input("Please enter that hour that Jimmy started lunch."))
lunchMinStart = int(input("Please enter the minute on the hour that Jimmy started lunch."))
lunchHourEnd = int(input("Please enter the hour that Jimmy ended his lunch break."))
lunchMinEnd =int(input("Please enter the minute on the hour that Jimmy ended his lunch break."))
start = hourStart * 60 + minStart
end = hourEnd * 60 + minEnd
totalTime = end + start
lunchStart = lunchHourStart * 60 + lunchMinStart
lunchEnd = lunchHourEnd * 60 + lunchMinEnd
lunchTime = lunchEnd - lunchStart
timeWorked = (totalTime - lunchTime) * 60
hoursWorked = int(timeWorked)
min = (timeWorked - hoursWorked) * 60
print (min)
Total time should be calculated as
totalTime = end - start
All durations are in minutes, so the following is already in minutes, and does not need to be multiplied by 60:
timeWorked = (totalTime - lunchTime)
Now you have timeWorked in minutes, so in hours that is:
hoursWorked = int(timeWorked / 60)
and the minutes would be the remainder:
minutesWorked = timeWorked % 60
The last 2 statements could be replaced with divmod():
hoursWorked, minutesWorked = divmod(timeWorked, 60)
Related
def computepay(hours, rate):
if hours > 40.0:
p = rate * 40.0
p = p+(1.5*rate*(hours-40))
else:
p = rate*hours
return p
hours = float(input("Enter worked hours: "))
rate = float(input("Enter Pay rate per hour: "))
print computepay(hours, rate))
I am getting E902 - EOF in multi-line statement on the def computepay.
First of all welcome to Stackoverflow.
And here is your code after I corrected with proper indentation and brackets.
It should work:
def computepay(hours, rate):
if hours > 40.0:
p = rate * 40.0
p = p+(1.5*rate*(hours-40))
else:
p = rate*hours
return p
hours = float(input("Enter worked hours: "))
rate = float(input("Enter Pay rate per hour: "))
print(computepay(hours, rate))
I'm working on a small personal project in Python 3 where you put in a name and get there weekly wages. I want to add a feature where an admin can come in and add a person to the workers, including their wages. Here's what I have as for people so far:
worker = input("Name: ")
if worker == "Anne":
def calcweeklywages(totalhours, hourlywage):
'''Return the total weekly wages for a worker working totalHours,
with a given regular hourlyWage. Include overtime for hours over 40.
'''
if totalhours <= 40:
totalwages = hourlywage * totalhours
else:
overtime = totalhours - 40
totalwages = hourlywage * 40 + (1.5 * hourlywage) * overtime
return totalwages
def main():
hours = float(input('Enter hours worked: '))
wage = 34
total = calcweeklywages(hours, wage)
print('Wages for {hours} hours at ${wage:.2f} per hour are ${total:.2f}.'
.format(**locals()))
main()
elif worker == "Johnathan":
def calcweeklywages(totalhours, hourlywage):
'''Return the total weekly wages for a worker working totalHours,
with a given regular hourlyWage. Include overtime for hours over 40.
'''
if totalhours <= 40:
totalwages = hourlywage * totalhours
else:
overtime = totalhours - 40
totalwages = hourlywage * 40 + (1.5 * hourlywage) * overtime
return totalwages
def main():
hours = float(input('Enter hours worked: '))
wage = 30
total = calcweeklywages(hours, wage)
print('Wages for {hours} hours at ${wage:.2f} per hour are ${total:.2f}.'
.format(**locals()))
main()
I want to add a part where if someone types in a code or something that they're an admin, it will let them add a person or edit an existing persons information.
I am not sure how you intend to deploy it, but even from the coding standpoint as it stands it will not behave the way you expect it to. I am guessing you are doing this just to learn. So, let me point out a couple of places where you have understood the basics wrong and a purely pedagogic example of how it could be done. Bear in mind that I strongly discourage from using this in any real context.
You have defined the function calcweeklywages twice in your code. When in fact it has to be defined just once. If you want to use the code, you call it, like you have done so in the main() program. The function works exactly the same for both your workers, so to get different weekly wages you pass different wages. But, how do you link their respective wages to their names (or some representation of them in your code)?
This is a good example where object oriented programming is used. A brief and entertaining primer is here. As for the code, it will look like this,
class Employee:
def __init__(self, Name, Wage = 0, Hours = 0):
self.Name = Name
self.Wage = Wage
self.Hours = Hours
def calcweeklywages(Employee, totalhours):
'''Return the total weekly wages for a worker working totalHours,
with a given regular hourlyWage. Include overtime for hours over 40.
'''
hourlywage = Employee.Wage
if totalhours <= 40:
totalwages = hourlywage * totalhours
else:
overtime = totalhours - 40
totalwages = hourlywage * 40 + (1.5 * hourlywage) * overtime
return totalwages
# In your main body, you just test the functionality
EmployeeList = []
EmployeeList.append(Employee("Anne", 34))
EmployeeList.append(Employee("Johnathan", 30))
while(True):
action = input('Exit? (y/n): ')
if(action == 'y'):
break
else:
name = input('Enter the employee\'s name: ')
for Employee in EmployeeList:
if(Employee.Name == name):
Person = Employee
hours = int(input('Enter the number of hours worked: '))
print('Wages for', hours, 'hours at', Person.Wage,'per hour is', calcweeklywages(Person, hours))
EDIT: I am sorry, I forgot about the admin part. But here goes,
class Employee:
def __init__(self, Name, Wage = 0, Hours = 0, Admin = False, code = ''):
self.Name = Name
self.Wage = Wage
self.Hours = Hours
self.Admin = Admin
self.code = code
def calcweeklywages(Employee, totalhours):
'''Return the total weekly wages for a worker working totalHours,
with a given regular hourlyWage. Include overtime for hours over 40.
'''
hourlywage = Employee.Wage
if totalhours <= 40:
totalwages = hourlywage * totalhours
else:
overtime = totalhours - 40
totalwages = hourlywage * 40 + (1.5 * hourlywage) * overtime
return totalwages
# In your main body, you just test the functionality
EmployeeList = []
EmployeeList.append(Employee("Anne", 34))
EmployeeList.append(Employee("Johnathan", 30))
EmployeeList.append(Employee("Mr. Admin", 50, 0, True, 'Open Sesame'))
while(True):
action = int(input('Enter action :\n 1. Exit.\n 2. Add new employee.\n 3. Compute weekly wage\n'))
if(action == 1):
break
elif(action == 2):
AdminName = input('Enter operator name : ')
Flag = False
for EmployeeInst in EmployeeList:
if((EmployeeInst.Name == AdminName) & (EmployeeInst.Admin)):
code = input('Enter code :')
if(code != EmployeeInst.code):
break
NewName = input('New Employee name? :')
NewWage = int(input('New employee wage? :'))
EmployeeList.append(Employee(NewName, NewWage))
Flag = True
if(not Flag):
print('Wrong Credentials')
break
elif(action == 3):
name = input('Enter the employee\'s name: ')
for Employee in EmployeeList:
if(Employee.Name == name):
Person = Employee
hours = int(input('Enter the number of hours worked: '))
print('Wages for', hours, 'hours at', Person.Wage,'per hour is', calcweeklywages(Person, hours))
else:
print('Input out of range')
break
But again, the session is not persistent between different kernel runs. There is no real "security", this is just an exploration of Python's object oriented code. Please do not use this for any real application. There is a lot more that goes with all this. You need to store it in a secure file, have some GUI front end etc etc. There are far wiser users who will guide you to implement the system as a whole. All the best with your studies. Cheers.
def cal_parking_fee (parking_duration):
if parking_duration< 60:
fee = 2.5
else:
fee = 1.80 + 2.50
return(fee)
hours = int(input("Enter minutes "))
total_fee = cal_parking_fee(hours)
print(total_fee)
Hi I'm trying to practice my python programming and I am stuck with this particular question. It says to Write/define a function called cal_parking_fee that takes in one parameter parking_duration in
minutes and return the fee payable based on the duration and parking rate. The rate is first hour of parking is $2.50 and every 30 min of parking thereafter is $1.80. I cant seems to make my code to work with the every 30 mins part.
def cal_parking_fee (parking_duration):
duration_mins = parking_duration
# duration_mins = parking_duration * 60
if duration_mins < 60:
fee = 2.5
else:
duration = duration_mins - 60
fee = 2.5
while duration > 0:
duration = duration - 30
fee = fee + 1.80
return(fee)
hours = int(input("Enter hours "))
total_fee = cal_parking_fee(hours)
print(total_fee)
Here's another approach
def cal_parking_fee (parking_duration):
fee = 2.5
if parking_duration > 60:
# Here I subtract 2 because the 1st two 30 minutes
# have a fixed fee of 2.5 that we've already set
if (parking_duration / 30 - 2) == 0:
periods_to_pay = 1
else:
periods_to_pay = (parking_duration / 30 - 2)
fee += periods_to_pay * 1.8
return(fee)
hours = int(input("Enter minutes "))
total_fee = cal_parking_fee(hours)
print(total_fee)
I just started college and I am writing this code out for my college class and it keeps sending an error. Any enlightenment will help, maybe the problem is that I'm half asleep.
here is the code
def main():
overtime = int(0)
totaloverpay = float(0)
hours = int(input('How many hours did you work? NOTE** Hours can not exceed 86 or be less than 8 '))
while hours > 86 or hours < 8:
print('ERROR: Insufficient input. Try again')
hours = int(input('How many hours did you work? NOTE** Hours can not exceed 86 or be less than 8 '))
payrate = float(input('What is the payrate per hour for this employee? NOTE** Payrate can not exceed $50.00 or be less than $7.00 '))
while payrate > 50.00 or payrate < 7.00:
print('ERROR: Insufficient input. Try again')
payrate = float(input('What is the payrate per hour for this employee? NOTE** Payrate can not exceed $50.00 or be less than $7.00 '))
workhours(hours, payrate, overtime)
def workhours(hours, payrate, overtime):
if hours > 40:
overtime = (hours - 40) * -1
else:
regtime = hours + 0
paydistribution(hours, payrate, regtime, overtime)
def paydistribution(hours, payrate, regtime, overtime):
if hours >= 40:
halfrate = float(payrate * 0.5)
overpay = halfrate + payrate
totaloverpay = float(overpay * hours)
if hours < 40:
regpay = hours * payrate
display(hours, payrate, regpay, regtime, overtime)
def display(hours, payrate, regpay, regtime, overtime):
total = float(regpay + totaloverpay)
print(' Payroll Information')
print('Payrate :', format(payrate, '0.2f'))
print('Regular Hours :', format(regtime))
print('Overtime Hours:', format(overtime))
print('Regular Pay :', format(regpay, '6.2f'))
print('Overtime Pay :', format(totaloverpay, '7.2f'))
print('Total Pay :', format(total, '7.2f'))
main()
totaloverplay is not defined in any function below main in which it is referred to, or as a global variable. If you want it to be global, define it outside of the main function's scope.
This looks like a great use-case for a class rather than relying on functional programming.
from decimal import Decimal # more precision than floating point
MINIMUM_WAGE = Decimal("7.25")
OVERTIME_RATE = Decimal("1.5")
class Employee(object):
def __init__(self,first,last,MI="",payrate=MINIMUM_WAGE):
self.first = first.capitalize()
self.last = last.capitalize()
self.MI = MI.upper()
if not MI.endswith("."): self.MI += "."
self.payrate = payrate
self.hours = 0
#property
def name(self, reversed_=False):
return "{} {} {}".format(self.first,self.MI,self.last)
#property
def alphabetize(self):
return "{}, {}".format(self.last, self.first)
def payroll(self,numweeks=1):
regularhours = min(40*numweeks,self.hours)
OThours = max(0,self.hours-regularhours)
regularpay = regularhours * self.payrate
OTpay = round(OThours * self.payrate * OVERTIME_RATE,2)
return {"reghours":regularhours,
"overtime":OThours,
"regpay":regularpay,
"OTpay":OTpay,
"total":regularpay + OTpay}
def sethoursworked(self,amt):
self.hours = amt
def display(employee):
payrollinfo = employee.payroll()
print("{:^30}".format("Payroll Information"))
print("{:>30}".format(employee.name))
print("Payrate:{:>22}".format(employee.payrate))
print("Hours:{:>24}".format(payrollinfo['reghours']))
print("Overtime Hours:{:>15}".format(payrollinfo['overtime']))
print("Regular Pay:{:>18}".format(payrollinfo['regpay']))
print("Overtime Pay:{:>17}".format(payrollinfo['OTpay']))
print("-"*30)
print("Total Pay:{:>20}".format(payrollinfo['total']))
Adam = Employee("Adam","Smith","D")
Adam.sethoursworked(51)
display(Adam)
OUTPUT:
Payroll Information
Adam D. Smith
Payrate: 7.25
Hours: 40
Overtime Hours: 11
Regular Pay: 290.00
Overtime Pay: 119.62
------------------------------
Total Pay: 409.62
You should not get in the habit of using global; it's usually a sign that you're heading in the wrong direction. Instead, pass the variables you need around explicitly, using function arguments and return statements. Also, don't pass functions arguments they don't need to do their job, and prefer default arguments or explicit constants to "magic numbers". For example:
def workhours(hours, threshold=40):
if hours > threshold:
overtime = hours - threshold
regtime = threshold
else:
overtime = 0
regtime = hours
return regtime, overtime
def paydistribution(payrate, regtime, overtime, otrate=1.5):
regpay = regtime * payrate
overpay = overtime * payrate * otrate
return regpay, overpay
Now main can call:
regtime, overtime = workhours(hours)
regpay, overpay = paydistribution(payrate, regtime, overtime)
display(hours, payrate, regpay, regtime, overtime)
This keeps the flow mostly in main while letting the other functions do just their specific bits of the task.
In your position, I would also consider having a separate function to take user input, which loops until they provide something acceptable. An appropriate definition, for example:
def user_input(prompt, min_, max_):
Here is another cleaned-up version:
from textwrap import dedent
import sys
if sys.hexversion < 0x3000000:
# Python 2.x
inp = raw_input
else:
# Python 3.x
inp = input
MIN_HOURS = 8.
MAX_HOURS = 86.
MIN_RATE = 7.
MAX_RATE = 50.
OVERTIME_CUTOFF = 40.
OVERTIME_BONUS = 0.5
def get_float(prompt, lo=None, hi=None):
while True:
try:
val = float(inp(prompt))
if lo is not None and val < lo:
print("Value must be >= {}".format(lo))
elif hi is not None and val > hi:
print("Value must be <= {}".format(hi))
else:
return val
except ValueError:
print("Please enter a number")
def main():
hours = get_float("How many hours did you work? ", MIN_HOURS, MAX_HOURS)
rate = get_float("What is the hourly payrate? ", MIN_RATE, MAX_RATE)
time = min(hours, OVERTIME_CUTOFF)
pay = time * rate
overtime = max(0., hours - OVERTIME_CUTOFF)
overpay = overtime * rate * (1. + OVERTIME_BONUS)
print(dedent("""
Payroll Information
Payrate : $ {rate:>7.2f}
Regular Hours : {time:>4.0f} h
Regular Pay : $ {pay:>7.2f}
Overtime Hours: {overtime:>4.0f} h
Overtime Pay : $ {overpay:>7.2f}
Total Pay : $ {total:>7.2f}
""").format(rate=rate, time=time, pay=pay, overtime=overtime, overpay=overpay, total=pay+overpay))
if __name__=="__main__":
main()
which runs like
How many hours did you work? 46
What is the hourly payrate? 11
Payroll Information
Payrate : $ 11.00
Regular Hours : 40 h
Regular Pay : $ 440.00
Overtime Hours: 6 h
Overtime Pay : $ 99.00
Total Pay : $ 539.00
Here's my program. There are seven employees for which I am creating a paystub. I am trying to achieve a loop where it starts at num = 1 and go all the way through num = 7. When I run the program, however, nothing gets printed. Thoughts?
#initalize all variables
medPremiere = 400
fedRate = .20
stateRate = .05
FICA = .08
retirement = .06
**Tax rates and such**
#the processing module
num = 1
while num < 8:
if num ==1:
empNum = 1
empName = 'billybob'
hours = 40
rate = 50
num = num + 1
if num ==2:
empNum = 2
empName = 'superman'
hours = 55
rate = 40
num = num + 1
if num ==3:
empNum = 3
empName = 'hulk'
hours = 60
rate = 60
num = num + 1
if num ==4:
empNum = 4
empName = 'scoobie'
hours = 45
rate = 80
num = num + 1
if num ==5:
empNum = 5
empName = 'Sherry'
hours = 66
rate = 30
num = num + 1
if num ==6:
empNum = 6
empName = 'doctor'
hours = 88
rate = 90
num = num + 1
if num ==7:
empNum = 7
empName = 'ironman'
hours = 77
rate = 70
num = num + 1
These are 7 different employees for which I have to create paystubs for
#the calc module
#calculate gross pay
num ==1
while num < 8:
They get payed overtime and double overtime so I have to account for how many hours each employee has worked. Less than 41 hours they get payed regular, 41-60 hours they get paid overtime and more than 61 hours they get payed double overtime.
if hours <41:
gross = rate*hours
fedTax = gross*fedRate
stateTax = gross*stateRate
F = gross*FICA
K = gross*retirement
netPay = gross - fedTax - stateTax - F - K - medPremiere
print('Gross pay: ', gross)
print('Federal tax # 20%: ', fedTax)
print('State tax # 5%: ', stateTax)
print('FICA # 8%: ', F)
print('401K # 6%: ', K)
print('Net pay: $', netPay)
num = num + 1
Here I'm trying to make it go back to the list of numbers at the top and pull the information for the next employee.
if hours < 61:
gross = (40*hours) + (hours - 40)(1.5)(rate)
fedTax = gross*fedRate
stateTax = gross*stateRate
F = gross*FICA
K = gross*retirement
netPay = gross - fedTax - stateTax - F - K - medPremiere
print('Gross pay: ', gross)
print('Federal tax # 20%: ', fedTax)
print('State tax # 5%: ', stateTax)
print('FICA # 8%: ', F)
print('401K # 6%: ', K)
print('Net pay: $', netPay)
num = num + 1
if hours > 61:
gross = 40*hours + (hours-40)(1.5)(rate) + (hours - 60)(2)(rate)
fedTax = gross*fedRate
stateTax = gross*stateRate
F = gross*FICA
K = gross*retirement
netPay = gross - fedTax - stateTax - F - K - medPremiere
print('Gross pay: ', gross)
print('Federal tax # 20%: ', fedTax)
print('State tax # 5%: ', stateTax)
print('FICA # 8%: ', F)
print('401K # 6%: ', K)
print('Net pay: $', netPay)
num = num + 1
break
Is the calc module properly formatted, or is there a better way to go about this?
Above the line with while num < 8: you say num ==1. This instead should be num = 1, and should be put inline with the while statement like so:
num = 1
while num < 8:
That's why none of the print statements execute; because num is not being reset to be less than 8.
Also, a stylistic point that can help you avoid errors (and it does the dirty work of initializing and incrementing counter vars for you), you can do this:
for num in range(1,8):
...
print num
...
This code can't possibly work as intended. You go over the first loop 7 times, rebinding the same variables and doing nothing else. At the end of that, you've got employee 7's values. Then you go over the second loop 7 times, using employee 7's values each time. (Then, because your indentation is incorrect, you don't do anything for an employee with >= 41 hours, so you do nothing 7 times.)
This is a very awkward way to structure your program, and it will be a lot easier to fix it if you restructure it.
First, if you want to loop over all of the numbers in [1, 8), use a for loop:
for num in range(1, 8):
This removes a whole lot of lines of extra code where you could get things wrong—including the one you actually did get wrong.
Next, you need to actually do something for each employee. While you could move the first loop into a function and put a yield empNum, empName, hours, rate, num after each one, this is making things far more complicated than they need to be. What you want is a function that can be called with a number and just return the right values for that number. Then you don't need a loop here at all.
But that function is already written for you, if you use the right data structure: it's just indexing.
For example, if you replace the first loop with this:
employees = {}
employees[1] = dict(empNum = 1,
empName = 'billybob',
hours = 40,
rate = 50)
employees[2] = dict(empNum = 2,
# etc.
… then the second loop can just do this:
for employee in employees.values():
if employee['hours'] < 41:
gross = employee['rate'] * employee['hours']
fedTax = gross*fedRate
stateTax = gross*stateRate
F = gross*FICA
K = gross*retirement
netPay = gross - fedTax - stateTax - F - K - medPremiere
print('Gross pay: ', gross)
print('Federal tax # 20%: ', fedTax)
print('State tax # 5%: ', stateTax)
print('FICA # 8%: ', F)
print('401K # 6%: ', K)
print('Net pay: $', netPay)
if employee['hours'] < 61:
# ...
But note that you're not actually using the keys for anything, so you must as well just use a list instead of a dict. (That way you also guarantee that you always iterate the employees in the same order you create them.) For example:
employees = [
dict(empNum = 1,
empName = 'billybob',
hours = 40,
rate = 50),
dict(empNum = 2,
# etc.
And now, you don't need for employee in employees.values():, just for employee in employees:.
Meanwhile, the indentation problem wouldn't be possible if you used elif and else. In this code:
if employee['hours'] < 41:
gross = employee['rate'] * employee['hours']
# ... a bunch more code
if employee['hours'] < 61:
gross = employee['rate'] * employee['hours']
# ... a bunch more code
if employee['hours'] > 61:
gross = employee['rate'] * employee['hours']
… everything compiles and runs, but you can never get into the last block, because hours can't be less than 41 and also be more than 60. But in this code:
if employee['hours'] < 41:
gross = employee['rate'] * employee['hours']
# ... a bunch more code
elif employee['hours'] < 61:
gross = employee['rate'] * employee['hours']
# ... a bunch more code
elif employee['hours'] > 61:
gross = employee['rate'] * employee['hours']
You will get a SyntaxError if you get the indentation wrong, because there's no if at the same level as the elif, which is easier to debug.
Next, note that 61 is not less than 41, or less than 61, or greater than 61, so nothing will happen for anyone who works 61 hours. You could fix that by using >= 61 for the last check. Or, even more simply, just using else instead of elif.
Next, whenever you write the same line of code more than twice, you should look for a way to refactor it. Almost all of the details are identical between the three cases; only the first line is different. Besides being harder to read, repetitive code is also harder to get right, and harder to maintain. For example, at some point, you're going to have a bug, and fix it in one copy but not the other two.
Also, you can't multiply two numbers in Python by adjoining them; you need to use the * operator, like this:
gross = 40*hours + (hours - 40) * 1.5 * rate + (hours - 60) * 2 * rate
Finally, your equations are wrong. If someone works 50 hours, you're going to pay them 40 hours at $1/hour because you forgot to multiply the first term by rate, and then you're going to pay them an extra 150% for the next 15 hours instead of an extra 50%.
Putting it all together, replace the first loop with the list of dictionaries above, then replace the second loop with this:
for employee in employees:
gross = rate * hours
if hours > 40:
gross += rate * (hours - 40) * 0.5 * rate
if hours > 60:
gross += rate * (hours - 60) * 0.5 * rate
fedTax = gross*fedRate
stateTax = gross*stateRate
F = gross*FICA
K = gross*retirement
netPay = gross - fedTax - stateTax - F - K - medPremiere
print('Gross pay: ', gross)
print('Federal tax # 20%: ', fedTax)
print('State tax # 5%: ', stateTax)
print('FICA # 8%: ', F)
print('401K # 6%: ', K)
print('Net pay: $', netPay)
And that's your whole program.