I have everything working accept when i calculate overtime it does the regular pay wrong.
where is my error I've been trying to figure out how to make it work for hours. If i put in 45 hours it has my regular pay X 45 not 40.
def main():
hours, rate = getinput()
strtime, overtimehr, overtime = calculate_hours (hours,rate)
regular, totalpay = calculate_payregular (hours, rate, overtime)
calprint (rate, strtime, overtimehr, regular, overtime, totalpay)
def getinput():
print ()
print ('How many hours were worked?')
print ('Hours worked must be at least 8 and no more than 86.')
hours = float (input('Now enter the hours worked please:'))
while hours < 8 or hours > 86: #validate the hours
print ('Error--- The hours worked must be atleast 8 and no more than 86.')
hours = float (input('Please try again:'))
print ('What is the pay rate?')
print ('The pay rate must be at least $7.00 and not more than $50.00.')
rate = float (input('Now enter the pay rate:'))
while rate < 7 or rate > 50: #validate the payrate
print ('Error--- The pay rate must be at least $7.00 and not more than $50.00.')
rate = float (input('Please try again:'))
return hours, rate
def calculate_hours (hours,rate):
if hours < 40:
strtime = hours
overtimehr = 0
else:
strtime = 40
overtimehr = hours - 40
if hours > 40:
overtimerate = 1.5 * rate
overtime = (hours-40) * overtimerate
else:
overtime = 0
return strtime, overtimehr, overtime
def calculate_payregular (hours, rate, overtime):
regular = hours * rate
totalpay = overtime + regular
return regular, totalpay
def calprint (rate, strtime, overtimehr, regular, overtime, totalpay):
print (" Payroll Information")
print ()
print ("Pay rate $", format (rate, '9,.2f'))
print ("Regular Hours ", format (strtime, '9,.2f'))
print ("Overtime hours ", format (overtimehr, '9,.2f'))
print ("Regular pay $", format (regular, '9.2f'))
print ("Overtime pay $", format (overtime, '9.2f'))
print ("Total Pay $", format (totalpay, '9.2f'))
main ()
Its because hours is never being redefined.
In main() you get the value of hours from getinput() - which is the total hours both standard and overtime.
hours is passed to calculate_hours(), but is never changed in the scope of main if it is over 40. So when you call calculate_payregular() the raw value of hours (which may be over 40) is passed in.
You can fix this, as calculate_hours() returns a normal-rate time in the returned variable strtime, so if you change this:
regular, totalpay = calculate_payregular (hours, rate, overtime)
to this:
regular, totalpay = calculate_payregular (strtime, rate, overtime)
It should calculate it correctly.
Related
I am designing a program that calculates your paycheck. It involves multiple functions and what I need to do is loop all of these functions from the order in which they are called. Here is my code without loops:
def showIntro():
intro = print('Hello! I will take your input and calculate your
weekly paycheck before
taxes.')
showIntro()
def get_rate():
rate = float(input('Enter hourly rate: '))
return rate
def get_hours():
hours = float(input('Enter hours worked: '))
if hours >= 41:
print('Your base hours including overtime is: ' , hours)
if hours <= 40:
print('Your hours entered were: ' , hours)
return hours
def get_paycheck(rate , hours):
paycheck = rate * hours
if hours >= 41:
print('Your weekly paycheck with over time is: ' , rate * hours)
if hours <=40:
print('Your weekly paycheck is: ' , rate * hours)
return (rate , hours)
rate = get_rate()
hours = get_hours()
get_paycheck(rate,hours)
end code
How do I loop this so it goes back to showIntro() and repeats itself.
You can put all function calls inside a while loop.
while True: # or some other condition
showIntro()
rate = get_rate()
hours = get_hours()
get_paycheck(rate, hours)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Please tell me what is wrong with it. because when I run this it tells an error. at line" if hrs > 40" and says its a syntax error!
while true:
hrs = input ("Enter no.of hrs worked: ")
rate = input ("Enter the rate per hour: ")
try:
hrs = int(hrs)
rate = int(rate)
raise ValueError("Non numeric value")
except enter code hereValueError as e:
print (e)
continue
if hrs > 40
# anything over 40 hrs earns the overtime rate
overtimeRate = 1.5 * rate
overtime = (hrs-40) * overtimeRate
# the remaining 40 hrs will earn the regular rate
hrs = 40
regular=hrs*rate
total_pay=regular+overtime
print(total_pay)
else:
# if you didn't work over 40 hrs, there is no overtime
overtime = 0
total_pay=hrs*rate
print(total_pay)
quit()
Your code contains some syntactic and semantic errors:
The boolean value true should start in capital letter True.
The way you write your code is very important, and should be formatted in the right way, the space before each instruction is sensitive i.e. the code in the same bloc should be preceded by the same number of space.
By using raise inside the try bloc, you created a custom exception that will always be executed and the if bloc will never be reached.
After the except key word you have two options:
Either to put the exception name, example:
except ValueError:
print("Non-numeric data found in the file.")
Or to not specify exception and let it blank
The correct way you should type your code is:
while True:
hrs = input ("Enter no.of hrs worked: ")
rate = input ("Enter the rate per hour: ")
try:
hrs = int(hrs)
rate = int(rate)
#raise ValueError("Non numeric value")
except :
print ('Non numeric data found.')
continue
if hrs > 40:
# anything over 40 hrs earns the overtime rate
overtimeRate = 1.5 * rate
overtime = (hrs-40) * overtimeRate
# the remaining 40 hrs will earn the regular rate
hrs = 40
regular=hrs*rate
total_pay=regular+overtime
print(total_pay)
else:
# if you didn't work over 40 hrs, there is no overtime
overtime = 0
total_pay=hrs*rate
print(total_pay)
quit()
Hope it helps!
I think this is what your looking for:
while True:
hrs = input ("Enter no.of hrs worked: ")
rate = input ("Enter the rate per hour: ")
try:
hrs = int(hrs)
rate = int(rate)
break
except ValueError as e:
print (e)
continue
if hrs > 40:
# anything over 40 hrs earns the overtime rate
overtimeRate = 1.5 * rate
overtime = (hrs-40) * overtimeRate
# the remaining 40 hrs will earn the regular rate
hrs = 40
regular=hrs*rate
total_pay=regular+overtime
print(total_pay)
else:
# if you didn't work over 40 hrs, there is no overtime
overtime = 0
total_pay=hrs*rate
print(total_pay)
quit()
A couple of mistakes...
You were missing a colon at the end of the if statement. Also, the else shouldn't be indented. Finally, I corrected your try except statement as you were raising the ValueError for no reason :). So now, if the inputs cannot be successfully converted to ints, then the ValueError will be raised and the loop will continue (if no error, then the code will continue and break out of the loop).
So the final code is:
while True:
hrs = input ("Enter no.of hrs worked: ")
rate = input ("Enter the rate per hour: ")
try:
hrs = int(hrs)
rate = int(rate)
break
except ValueError:
print("invalid entries")
continue
if hrs > 40:
# anything over 40 hrs earns the overtime rate
overtimeRate = 1.5 * rate
overtime = (hrs-40) * overtimeRate
# the remaining 40 hrs will earn the regular rate
hrs = 40
regular=hrs*rate
total_pay=regular+overtime
print(total_pay)
else:
# if you didn't work over 40 hrs, there is no overtime
overtime = 0
total_pay=hrs*rate
print(total_pay)
You should not use raise but just try except + use correct indentation (if an else) would be never used in your case (only while loop). Try version:
while True:
hrs = input ("Enter no.of hrs worked: ")
rate = input ("Enter the rate per hour: ")
try:
hrs = int(hrs)
rate = int(rate)
except ValueError as e:
print (e)
print("Non numeric value")
continue
if hrs > 40:
# anything over 40 hrs earns the overtime rate
overtimeRate = 1.5 * rate
overtime = (hrs-40) * overtimeRate
# the remaining 40 hrs will earn the regular rate
hrs = 40
regular=hrs*rate
total_pay=regular+overtime
print(total_pay)
else:
# if you didn't work over 40 hrs, there is no overtime
overtime = 0
total_pay=hrs*rate
print(total_pay)
I keep getting the following error and my program will not run. I need to make sure my program is modular and have the if-then statements to figure out what gross pay equation to use.
BASE_HOURS = 40
OT_MULTIPLIER = 1.5
def main():
hours = input("Enter the number of hours worked: ")
payRate = input("Enter the hourly pay rate: ")
calcPayWithOT(hours,payRate)
def calcPayWithOT(hours,payRate):
if hours <= BASE_HOURS:
theGrossPayNoOT = hours * payRate
print("The gross pay is $ ", theGrossPayNoOT)
if hours > BASE_HOURS:
theGrossPayOT = (hours - BASE_HOURS) * OT_MULTIPLIER + (hours * payRate)
print("The gross pay is $ ", theGrossPayOT)
main()
You should convert the hours and payRate into integers or floats like so:
hours = int(input("Enter the number of hours worked: "))
payRate = int(input("Enter the hourly pay rate: "))
or
hours = float(input("Enter the number of hours worked: "))
payRate = float(input("Enter the hourly pay rate: "))
Depending if you want to include only natural numbers or ones with figures after the decimal .
I'm working on this simple task where I need to use 2 while loops. The first while loop checks if the number of hours is less than 0 if it is then loop should keep asking the user.
Here's my code:
hours = float(input('Enter the hours worked this week: '))
count = 0
while (0 > hours):
print ('Enter the hours worked this week: ')
count = count + 1
pay = float(input('Enter the hourly pay rate: '))
while (0 > pay):
print ('Enter the hourly pay rate: ')
count = count + 1
total_pay = hours * pay
print('Total pay: $', format(total_pay, ',.2f'))
break is what you're looking for.
x = 100
while(True):
if x <= 0:
break
x -= 1
print x # => 0
As for your example, there is nothing that would seem to cause a break to occur. For example:
hours = float(input('Enter the hours worked this week: '))
count = 0
while (0 > hours):
print ('Enter the hours worked this week: ')
count = count + 1
You are not editing the hours variable at all. This would merely continue to print out "Enter the hours worked this week: " and increment count ad infinitum. We would need to know what the goal is to provide any more help.
You exit a loop by either using break or making the condition false.
In your case, you take input from the user, and if hours < 0, you print the prompt and update the count, but you don't update hours.
while (0 > hours):
print ('Enter the hours worked this week: ')
count = count + 1
should be:
while (0 > hours):
hours = float(input('Enter the hours worked this week: '))
count = count + 1
Similarly for pay:
while (0 > pay):
pay = float(input('Enter the hourly pay rate: '))
count = count + 1
Well, the other answer shows you how to break out of a while loop, but you're also not assigning to the pay and hours variables. You can use the built-in input function to get what the user supplied into into your program
hours = float(input('Enter the hours worked this week: '))
count = 0
while (0 > hours):
hours = input('Enter the hours worked this week: ')
count = count + 1
pay = float(input('Enter the hourly pay rate: '))
while (0 > pay):
pay = input('Enter the hourly pay rate: ')
count = count + 1
total_pay = hours * pay
print('Total pay: $', format(total_pay, ',.2f'))
I am trying to use Python coding to create a paycheck when a user enters a said amount of hours. For under 40 hours a week of work, the standard pay is $9.25 an hour. Anything over 40 hours is rewarded a 150% of bonus(150% of 9.25 that is). I should also be able to take in hours entered in fractions and be able to print the paycheck as the output(which states the number of hours worked, pay per hour, bonus received, if any and the total salary)
So far, I have been able to successfully use the if else statement to get the final result but I do not know how to take in fractions and print the entire paycheck in the output. I am fairly new to Python and really like it a lot. Can someone please help me improvise my code and help me with fractions and printing the entire paycheck?
Here is my code so far.
hours = int(input('Please enter the number of hours...'))
if hours <= 40:
hourlyWage = hours*(9.25)
elif hours > 40:
hourlyWage = hours*(9.25*1.5)
print('Your salary is ${0}'.format(hourlyWage))
Thank you and help is much appreciated!
hours = int(input('Please enter the number of hours...'))
hourlyWage = 9.25
bonus = 0.5
bonusThreshold = 40
bonusHours = max(0, hours - bonusThreshold)
regularSalary = hourlyWage * hours
bonusSalary = bonusHours * hourlyWage * bonus
totalSalary = regularSalary + bonusSalary
print('Worked: {0} hours.'.format(hours))
print('Pay per hour: ${0}.'.format(hourlyWage))
print('Bonus received: ${0}'.format(bonusSalary))
print('Total salary: ${0}'.format(totalSalary))
hours = float(input('Please enter the number of hours...'))
hourlyWage = hours*(9.25)
hours = hours - 40
if hours > 0:
hourlyWage = hourlyWage + (hours*(9.25*1.5))
print('Your salary is ${0}'.format(hourlyWage))
hours = float(input('Please enter the number of hours...'))
pay = hours * 9.25 + max(hours-40,0)* (9.25*1.5-9.25)
print('Your salary is ${0}'.format(pay))
Your need to format your print statement. Here are some examples.
hours = 50
hourly_wage = 9.25
>>> print('Your salary is ${0:,.2f}'.format(hourly_wage * hours
+ max(0, hours - 40) * hourly_wage * 1.5))
Your salary is $601.25