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)
Related
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
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.
Ask the user to enter payroll information for the company. Set up a loop that continues to ask for information until they enter “DONE”. For each employee ask three questions:
name (first & last)
hours worked this week (only allow 1 through 60)
hourly wage (only allow 6.00 through 20.00)
VALIDATE the hours worked and the hourly wage, and make sure a name is entered.
Calculate each employee’s pay, and write it out to a sequential file. Be sure to include file I/O error handling logic.
Include only the weekly pay
Weekly pay is calculated:
For (1-40 hours) it is hourly rate * hours worked
For (41-60 hours) it is (hours worked – 40) * (hourly rate * 1.5)
+ hourly rate * 40
After all the employees are entered, read in the sequential file into a list named PAY for the weekly pay of each employee. Sort the list. Now print the lowest, highest, and average weekly pay for the week.
I am having obvious problem with this code
while len(eName)>0:
eName=raw_input("\nPlease enter the employees' first and last name. ")
hWork=raw_input("How many hours did they work this week? ")
hoursWork=int(hWork)
if hoursWork < 1 or hoursWork > 60:
print "Employees' can't work less than 1 hour or more than 60 hours!"
else:
pRate=raw_input("What is their hourly rate? ")
payRate=int(pRate)
if payRate < 6 or payRate > 20:
print "Employees' wages can't be lower than $6.00 or greater than $20.00!"
if hoursWork <=40:
grossPay=hoursWork*payRate
else:
grossPay=((hoursWork-40)*(payRate*1.5))+(40*payRate)
lsthours.append(grossPay)
print grossPay
print lsthours
ePass=raw_input("Type DONE when finished with employees' information. ")
ePass.upper() == "DONE"
if ePass == "DONE":
break
else:
continue
There's several problems with this code:
The indentation is all over the place. For example, the while loop ends at that first if statement
The test for the while loop is almost certainly false (since eName isn't initialised), so the loop never enters
the code at ePass.upper() == "DONE" is trying to set the ePass variable, which means that test won't work. You need:
if ePass.upper() == "DONE":
break
try this:
lsthours = list()
eName = "start" # initialize to something to start the loop
while eName:
eName = raw_input("\nPlease enter the employees' first and last name. ")
if not eName:
break #loop will exit also when blank name is inserted
hWork = raw_input("How many hours did they work this week? ")
hoursWork = int(hWork)
if hoursWork < 1 or hoursWork > 60:
print "Employees' can't work less than 1 hour or more than 60 hours!"
continue #skip
pRate = raw_input("What is their hourly rate? ")
payRate = int(pRate)
if payRate < 6 or payRate > 20:
print "Employees' wages can't be lower than $6.00 or greater than $20.00!"
continue #skip
if hoursWork <= 40:
grossPay = hoursWork * payRate
else:
grossPay = ((hoursWork - 40) * (payRate * 1.5)) + (40 * payRate)
lsthours.append(grossPay)
print grossPay
print lsthours
ePass = raw_input("Type DONE when finished with employees' information. ")
if ePass.upper() == "DONE":
break
It still lacks exception checking but should work.
The "data error" checks should just short-circuit the main loop, it's simpler, but you can have a more involved code and put them into their own loop.
A few errors as has been pointed out:
In python, indentation decides the code blocks
while loop:
while logic_test:
# this is inside while loop
....
# this is outside while loop
Certain functions on string does not replace the string in place, they return another string via return value
upper:
>>> a = "done"
>>> a.upper()
'DONE'
>>> a
'done'
>>>
Always initialize your variables.
If you are using sequence methods, the variable should have been defined as sequence earlier.
>>> t.append('ll')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 't' is not defined
>>> t = []
>>> t.append('ll')
>>>
Make your scope explicit
lsthours = []
while len(eName)>0:
........
lsthours.append(grossPay)
Yu can do something as this:
grossPay = 0.0
lsthours = []
eName=raw_input("\nPlease enter the employees' first and last name (type 'PASS' to exit): ")
while eName.upper() != "PASS":
hWork=raw_input("How many hours did they work this week? ")
hoursWork=int(hWork)
if hoursWork < 1 or hoursWork > 60:
print "Employees' can't work less than 1 hour or more than 60 hours!"
else:
pRate=raw_input("What is their hourly rate? ")
payRate=int(pRate)
if payRate < 6 or payRate > 20:
print "Employees' wages can't be lower than $6.00 or greater than $20.00!"
if hoursWork <=40:
grossPay=hoursWork*payRate
else:
grossPay=((hoursWork-40)*(payRate*1.5))+(40*payRate)
lsthours.append(grossPay)
print grossPay
print lsthours
eName=raw_input("\nPlease enter the employees' first and last name. (type 'PASS' to exit): ")