Spring 2006 Midterm 1 Review: Compute the pay rate
Spring 2006 Midterm 1 Review: Compute the pay rate here.
Here is my program, and it computes both the gross pay and the pay received after tax. However, I was not able to make it print out "gross pay = #" and "net pay =#" but just the numbers alone. I put into the programming area [prnt = "gross"+"pay"+hoursWorkedpayRate] but that did not work. Any suggestions?
def pay(hoursWorked,payRate):
grossPay=hoursWorked*payRate
print grossPay
if(grossPay<100):
netPay=grossPay*.75
if(grossPay>=200 and grossPay300):
netPay=grossPay*.7
if(grossPay>=200 and grossPay<=300):
netPay=grossPay*.65
if(grossPay>=300 and grossPay400):
netPay=grossPay*.55
if(grossPay>400):
netPay=grossPay.5
print netPay
Thanks
Paul
Woops, hoursWorkedpayRate, and no equal sign after prnt also on last post
Paul
| Paul, you could say print ("gross = " + str(grossPay) + " net pay = " + str(netPay)) Amanda Bennett |
Have we even done this in class?
| Yes and No.You have not seen this exact example however you have learned the tools needed to solve problems like this. Instead of values of RBG we are using regular numbers. -Albert d'Heurle |
def pay(hours,hourlyrate):
grosspay=hours*hourlyrate
if (grosspay<100):
netpay=(grosspay-.25*grosspay)
if (grosspay >=100 and grosspay <200):
netpay=(grosspay-.30*grosspay)
if (grosspay >=200 and grosspay <300):
netpay=(grosspay-.35*grosspay)
if (grosspay >=300 and grosspay <400):
netpay=(grosspay-.45*grosspay)
if (grosspay >=400):
netpay=(grosspay-.50*grosspay)
print ("grosspay=" + str(grosspay) + "netpay=" + str(netpay))
Would this be counted correct? I know it's more complicated than Paul's since he did the netpay=grosspay(1.00-charge a tax of)
Oh and in Paul's code, the second if statement should read if (grosspay >=100 and grosspay 200):
He repeated the third if statement.
if (grosspay >=100 and grosspay <200):
def pay(hours, rate):
gross = hours*rate
print gross
if gross<100:
tax = .25*gross
print tax
if gross>=100 and gross<200:
tax = .3*gross
print gross - tax
if gross>=200 and gross<300:
tax = .35*gross
print gross - tax
if gross>=300 and gross<400:
tax = .45*gross
print gross - tax
if gross>400:
tax = .5*gross
print gross - tax
This seems simpler than the upper two, mostly because it involves "print" within the "if" statements. Is it still okay? It has worked for me on JES.
How come for the first if statement you did not put print gross-tax and you simply put print tax?
| This is a matter of style. Generally, it's considered better (i.e. easier to understand and modify) if you put together all the things that belong together. So it would be better to have all the printing done in one place rather than inside all the ifs. Having print statements everywhere in a commercial program would not be so good if the customer said they wanted the output formatted differently. You would have to make lots of similar changes instead of one change in one place. And that's a recipe for making mistakes. On the other hand 'simpler' can also mean 'easier to write and get working in the first place.' And if you are saying that your version is simpler in this sense, then you must be right. So, do what works for you. Colin Potts |
Link to this Page