Jump to content

Learning Python


Jklein

Recommended Posts

Ok I'm trying to learn python and I have come across a problem I need to solve for the exercise I'm doing in teachig yourself python in 24 hours lesson 2

 

I' supposed to wwrite a single line of code that solves this problem: I'm buying office supplies the total is 10 dollars but I have a 30% percent discount and I need to add 5% sales tax and 7.85 for shipping

 

This is how I'm thinking it should go:

 

10 - 10 * .30 (thats going to give me the price after discount which is 7 dollars) so I should then take that formula and add on the sales tax:

 

10 - 10 * .30 + 10 - 10 * .30 * .05 (the problem is I wind up with 16.85 which isnt right because the whole problem should equal $15.20)

 

So anybody have a clue where I'm going wrong

 

 

 

 

Share this post


Link to post
Share on other sites

Your problem isn't an issue with Python, you aren't taking into account the order of operations for math.

 

 

10 - 10 * .30 + 10 - 10 * .30 * .05 (the problem is I wind up with 16.85 which isnt right because the whole problem should equal $15.20)

 

Can be rewritten as 10 - (10*.30) + 10 - (10*.30*.05), which is giving you the incorrect amount.

 

In other words, you need to properly format the math to get it to do what you want.

Share this post


Link to post
Share on other sites

you guys are so smart and hip... i'm amazed

 

someone needs to tell my missus that knowledge of operation precedence is hip ;)

 

codeacademy do python courses if you get through your book and want more.

Share this post


Link to post
Share on other sites

ok so I wrote it like this:

 

10 - (10 * .30) + 10 - (10 * .30 * .05) + 7.50  

 

and I got 24.35

but were starting out with 10 in supplies subtracting 30% discount adding on 5% sales tax and 7.50 for shipping so it should be 15.20 from what I understand so I still don't know whats going on 

Share this post


Link to post
Share on other sites

when doing percentages it's better to keep the terms collected.

for example, for doing the discount, 10*(1. - 0.3) is easier to understand than 10 - 10*0.3 even though it's the same thing.

if you do the same for tax then add shipping you should get the answer.

 

also was shipping 7.85 or 7.5?

 

did you calculate the 15.20 value or was it given to you?

if you calculated it just write down on paper exactly what you did and then put that directly in python (also you don't need to expand out all the terms like you've been doing)

 

I generally find it's better to figure out exactly each step of a problem (on paper) before starting to put it into code because if you don't know exactly what to do the computer definitely won't

Share this post


Link to post
Share on other sites

You need more parentheses. Take a piece of paper and write out the math step by step. Each step you do, needs to be in parentheses.

 

Answer:

print((10-(10*0.3))+((10-(10*0.3))*0.05)+7.85)

Edited by AZNguyen

Share this post


Link to post
Share on other sites

ok so I wrote it like this:

 

10 - (10 * .30) + 10 - (10 * .30 * .05) + 7.50  

 

and I got 24.35

but were starting out with 10 in supplies subtracting 30% discount adding on 5% sales tax and 7.50 for shipping so it should be 15.20 from what I understand so I still don't know whats going on 

 

You rewrote it like that, but it is still incorrect. That is the same exact math that you had in the first post which is why you got $24.35. $16.85+$7.50=$24.35. My point was that your math was incorrect.

Share this post


Link to post
Share on other sites

jklein what you just wrote should be : 10*(1-.30)+10*(1-.30)*.05 + 7.50 (one of the brackets was in the wrong place)

 

what I was alluding to is:

10*(1. - 0.3)*(1. + 0.05) + shipping (7.5 or 7.85?)

I'm guessing that's what waco meant as well

 

I think writing it like that makes everything clearer

you start with 10,

apply the discount 10*(1. - 0.3) ,

then apply tax to the discounted price ( 10*(1. - 0.3) )*(1. + 0.05) = 10*(1. - 0.3)*(1. + 0.05),

then add shipping

giving the answer of 14.85 or 15.2 (depending on shipping)

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...