Midterm Exam 1 Review Spring 2004: Compute the pay rate #2
(Return to Sp2004 Midterm 1 Review)
Questions, answers, comments on answers, comments on questions?
Amelia Cipolla
def pay(hoursWorked, hourlyRate)
if grossPay<100:
taxAmount=(grossPay*0.25)
if grossPay >199 and grossPay <300:
taxAmount=(grossPay*0.35)
if grossPay >299 and grossPay <400:
taxAmount=(grossPay*0.45)
if grossPay >399
taxAmount=(grossPay*0.50)
print grossPay
netPay=(grossPay-taxAmount)
print netPay
Is that what you were talking about??
| Your logic is good, but you have a few problems with the syntax (structure of the language). For the < 300 and < 400, what are you comparing the numbers to? It's obvious to a human that you want to compare them to grossPay, but Jython doesn't know that. All Jython knows is that that there should be a number to the left and right of <. If one of them isn't there, it's not going to know what you mean. Also, to nitpick, you need to print out grosspay and netpay. Adam Wilson |
| For the benefit of other readers, someone (Amelia?) must have edited the code directly since Adam made those comments, because the comparisons are correct and the values are printed. Colin Potts |
| One more thing, at least for the version that is now up, grosspay has not been defined. You need to define grosspay, otherwise JES will give you an error. Houman Khalili |
I cannot figure out how to get my name printed after I comment, sorry. My name is Kyla LeCroy and I did the following:
def pay(hoursworked,hourlyrate)
grosspay=hoursworked*hourlyrate
tax=taxrate*grosspay
netpay=(gross-tax)
if(grosspay<100):
taxrate=0.25
if(grosspay>=200 and<300):
taxrate=0.35
if(grosspay>=300and<400):
taxrate=0.45
if(grosspay>=400):
taxrate=0.50
print grosspay
print netpay
is this correct? It's slightly different from Amelias...did I put something in the wrong place?
| Kyla, to get your name to show up, just type *Kyla LeCroy*, and if you created your Who's Who page with your name, the link will show up to that page. One thing wrong that I see in your code is that you compute the tax and the netpay BEFORE you compute the taxrate. It won't work that way – you'll get an error that the taxrate is a "local or global name not found." Mark Guzdial |
Here's what I came up with:
def pay(hours, wage):
grossPay = hours * wage
if (grossPay < 100):
tax = .25
if (grossPay >= 200 and < 300):
tax = .35
if (grossPay >= 300 and < 400):
tax = .45
if (grossPay >= 400):
tax = .50
netPay = grossPay - (tax * grossPay)
print grossPay
print netPay
I haven't run it yet, though. Andrea Dunlop
| Your syntax for the second and third conditional (if) statements don't seem right. Take a look at previous postings. Houman Khalili |
Clarke Collins
def pay(hours,rate):
grossPay=(hoursrate)
if grossPay100:
tax=(grossPay0.25)
if grossPay >=200 and grossPay 300:
tax=(grossPay0.35)
if grossPay >=300 and grossPay 400:
tax=(grossPay0.45)
if grossPay >=400:
tax=(grossPay0.50)
print grossPay
netPay=(grossPay-tax)
print netPay
I was able to get this one to work in JES. Do you see any problems?
I used cut and paste from JES program. Indents appear to have been lost. Let me try again. Clarke Collins
def pay(hours,rate):
grossPay=(hours*rate)
if grossPay<100:
tax=(grossPay*0.25)
if grossPay >=200 and grossPay <300:
tax=(grossPay*0.35)
if grossPay >=300 and grossPay <400:
tax=(grossPay*0.45)
if grossPay >=400:
tax=(grossPay0.50)
print grossPay
netPay=(grossPay-tax)
print netPay
Okay, what did I do wrong? Indents are lost and multiply symbol was deleted? Clarke Collins
| Clarke read the faq page. You have to use "html" and "pre" tags to preserve the indents. I went ahead and fixed it up for you. Greg Leo |
Amy Howard
def pay(hoursworked, rate):
grosspay = hoursworked*rate
if grosspay < 100:
taxedamount = .25*grosspay
if grosspay >= 200 and grosspay < 300:
taxedamount = .35*grosspay
if grosspay >= 300 and grosspay < 400:
taxedamount = .45*grosspay
if grosspay >= 400:
taxedamount = .50*grosspay
netpay = grosspay - taxedamount
print grosspay
print netpay
| This actually won't even load, Amy. Python is very particular about the indentation. The "if"'s, grosspay=..., and netpay=... all have to line up. Mark Guzdial |
Laura Bosworth
def pay(hours,rate):
wage = (hours*rate)
if (wage < 100):
netPay = (wage - (wage*.25))
if (wage >= 200 and wage <300):
netPay = (wage - (wage*.35))
if (wage >= 300 and wage <400):
netPay = (wage - (wage*.45))
if (wage >=400):
netPay= (wage - (wage.50))
print netPay
print wage
How does this code look?
| You could always test it in JES and then tell us whether it works or not...:-) Mark Guzdial |
Houman - I caught that when I tried to run it. Drove me crazy trying to figure it out! Thanks. Andrea Dunlop
Okay, let me try this again...this is what I have now:
def pay(hoursworked,hourlyrate)
if(grosspay100):
taxrate=0.25
if(grosspay>=200 and300):
taxrate=0.35
if(grosspay>=300and400):
taxrate=0.45
if(grosspay>=400):
taxrate=0.50
grosspay=hoursworkedhourlyrate
tax=taxrategrosspay
netpay=(gross-tax)
print grosspay
print netpay
this should work, does anyone see anything wrong with it? :) Kyla LeCroy
daniel goers
wrote this and it worked with made up values
def pay(hours,rate):
grosspay = hours*rate
if grosspay < 100:
taxrate = .25
if grosspay >= 200 and grosspay < 300:
taxrate = .35
if grosspay >= 300 and grosspay < 400:
taxrate = .45
if grosspay >= 400:
taxrate = .50
taxtotal = grosspay * taxrate
netpay = grosspay - taxtotal
print grosspay
print netpay
def pay (hours, wage):
grosspay = hours*wage
if (grosspay < 100 ):
tax=.25
if (grosspay >= 200 and grosspay < 300 ):
tax=.35
if (grosspay >= 300 and grosspay < 400 ):
tax = .45
if (grosspay >= 400 ):
tax = .50
ttax= tax*grosspay
netpay = grosspay - totaltax
print grosspay
print netpay
I can't find what's wrong with this. JES keeps popping up: A local name was used before it was created. You need to define the method or variable before you try to use it. in the line ttax= taxNick Faulconer_
That was it thanks
| What input are you using, Nick? Perhaps you're specifying a grosspay of something like 100, which doesn't match any of the IF's, so tax never gets a value?Mark Guzdial |
htmldef pay(hoursWorked, hourRate):
grossPay=hoursWorkedhourRate
if grossPay 100:
tax = grossPay .25
if grossPay >= 200 and grossPay 300:
tax = grossPay .35
if grossPay >= 300 and grossPay 400:
tax = grossPay .45
if grossPay >= 400:
tax = grossPay .50
value = grossPay-tax
print grossPay
print value
/pre/html
so i'm thinking this code is right. or atleast it's spitting out my grossPay value, but the problem is that it is telling me that my value=grossPay-tax has not been predefined.... anyone HELP???Arseni Zaitsev|What input are you specifying? See my answer to Nick above.Mark Guzdial|
Yeah so i think i girgured it out, if you set first value <100 and then next tax cal at >=200, then anynumber between 100 and 200 is invalid. so MARK, shouldn't the value be (if the par is less then 200 set it to .25)??
| That would catch more gross pay values, yup. That doesn't mean that those values are iinvalid/i, just that they don't have a corresponding tax rate.Mark Guzdial |
pre
def pay(HoursWorked, PayRate):
GrossPay = HousrsWorked* PayRate
if GrossPay < 100:
TaxRate = 0.25
elif (GrossPay >= 200) and (GrossPay <300):
TaxRate = 0.35
elif (GrossPay >= 300) and (GrossPay < 400):
TaxRate = 0.45
else:
TaxRate = 0.50
TaxAmount = GrossPay * TaxRate
print GrossPay, TaxAmount
/html*Rachel King
Mine worked above when I ran it in JES so I guess it looks good?? Let me know if anyone can find something wrong with it! *1089*
_
_
def pay(work,rate):
gross=work*rate
if(gross<100):
tax=gross*.25
if(gross>=200 and gross <300):
tax=gross*.35
if(gross>=300 and gross<400):
tax=gross*.45
if(gross>=400):
tax=gross*.50
netpay=gross-tax
print gross
print netpay
Am I on the right track here? This worked in JES, but is it an acceptable solution? Bryan W. Grant
Link to this Page