# Learning Python

Coding Python

10 replies to this topic

### #1 Jklein

Jklein

Member

• Members
• 151 posts
• Gender:Male

Posted 01 May 2014 - 10:47 PM

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

### #2 CheeseMan42

CheeseMan42

Certified NVIDIA Fanboy

• Review Editor
• 4377 posts
• Gender:Male
• Location:Bristol, CT

Posted 02 May 2014 - 01:34 AM

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.

i7-6700K @ 4.2GHz | Noctua NH-D14 | MSI Gaming M7 | 2x8GB G.Skill Ripjaws V | GTX 980 Ti | 120GB Samsung 850 EVO M.2 | Corsair 750TX | CM Stacker 830 | SoundBlaster X-Fi Titanium | Sennheiser HD650 + Schiit Magni/Modi 2 + Logitech G430 | Logitech G500s | CM Quick Fire Rapid

Intel i3 NUC | 8GB RAM | Samsung 120GB M.2 || Samsung Chromebook || Google Nexus 7

SteamFor Sale Thread | OCC Steam Group | Follow OCC on
Gr4vitas (3/18/09) - So moral of the story is. Vista 64 bit makes me happy in the pants.

### #3 cjloki

cjloki

Demigod

• Members
• 3193 posts
• Gender:Male
• Location:Riverside CA.

Posted 02 May 2014 - 06:22 AM

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

#1) ASUS X99-A I7 5930@4.0ghz,CoolerMaster Hyper 212, 48gb Corsair Vengence @2400, ASUS 1080 Strix Edition, 1tb Samsung 850 EVO and 2x600gb WD velociraptors, Sony OptiarcDVD rw, Antec 1200 Full tower,Corsair 1050,w10pro64, gaming

#3) Gigabyte p55ud5+I-7 875k@2.93turbo, 4g gskill PIS 2200, nvidia gtx650, corsair h50, samsung f3 1tb, lg supermultiDVD, coolermaster 500w psu, raidmax sieran, win8pro64 clone, Sheila's office machine

Bloodbath

Warweeny: " just let it go, be free and relax ! "

### #4 suedenim

suedenim

Certified Geek

• Members
• 1912 posts
• Gender:Male
• Location:Somerset

Posted 02 May 2014 - 06:40 AM

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.

1055x6 | ASUS 890GX | 4GB G.Skill ECO | 5850 Vapor X | 2007WFP x 3 | Antec P190 | NH-D14

### #5 Jklein

Jklein

Member

• Members
• 151 posts
• Gender:Male

Posted 02 May 2014 - 01:25 PM

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

### #6 peter_thm

peter_thm

New Member

• Members
• 10 posts
• Gender:Male
• Location:Brisbane

Posted 02 May 2014 - 07:20 PM

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

i7-2700 | CM Hyper 212 EVO | 16GB G.Skill Ripjaws-X 1600 | Sapphire HD 7850 | 240GB Sandisk Extreme II | 2TB + 1TB WD Black | Seasonic X-660 | Lian Li PC-7FNWX

Benq XL2410T | LG 23EA53 | Ducky Shine Zero (Cherry Brown) | Mionix Naos 8200 | Razer Lachesis 5600

### #7 WhenKittensATK

WhenKittensATK

Melting pot of friendship

• Members
• 8233 posts
• Gender:Male
• Location:FL

Posted 02 May 2014 - 08:16 PM

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.

Spoiler

Edited by AZNguyen, 02 May 2014 - 08:30 PM.

Gaming Rig || Intel i7 3770K || Gigabyte Z68X-UD3H-B3 || G.Skill 8GB || MSI GTX 1070 || Corsair TX750 V2 ||

Peripherals || ASUS PG279Q || Corsair Vengeance K90 || Logitech G700s || Blue Snowball ||

Audio Gear || Schiit Bitfrost 4490 || Schiit Lyr 2 || Sennheiser HD6XX || Monoprice 605500 ||

NAS || Intel Xeon E3-1275 v5 || Supermicro X11SSH-LN4F || Samsung 16 GB ECC RAM || SeaSonic X-Series 750W || 54 TB Raw Storage ||

### #8 CheeseMan42

CheeseMan42

Certified NVIDIA Fanboy

• Review Editor
• 4377 posts
• Gender:Male
• Location:Bristol, CT

Posted 03 May 2014 - 04:31 AM

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.

i7-6700K @ 4.2GHz | Noctua NH-D14 | MSI Gaming M7 | 2x8GB G.Skill Ripjaws V | GTX 980 Ti | 120GB Samsung 850 EVO M.2 | Corsair 750TX | CM Stacker 830 | SoundBlaster X-Fi Titanium | Sennheiser HD650 + Schiit Magni/Modi 2 + Logitech G430 | Logitech G500s | CM Quick Fire Rapid

Intel i3 NUC | 8GB RAM | Samsung 120GB M.2 || Samsung Chromebook || Google Nexus 7

SteamFor Sale Thread | OCC Steam Group | Follow OCC on
Gr4vitas (3/18/09) - So moral of the story is. Vista 64 bit makes me happy in the pants.

### #9 Jklein

Jklein

Member

• Members
• 151 posts
• Gender:Male

Posted 03 May 2014 - 11:35 AM

Ok this is what I did:

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

The answer is 14.985 which you round up to \$14.99

### #10 Waco

Waco

Lab Rat 2

• Members
• 16508 posts
• Gender:Male
• Location:Los Alamos, NM

Posted 03 May 2014 - 11:42 AM

What's wrong with:

10 * ((1 - .3) + .05) + 7.85? Easiest to program too...

Tolerance is a sign of weakness.

### #11 peter_thm

peter_thm

New Member

• Members
• 10 posts
• Gender:Male
• Location:Brisbane

Posted 03 May 2014 - 10:13 PM

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