Why is this correct? Looks to me like it shouldn't work. - Overclock.net - An Overclocking Community

Forum Jump: 

Why is this correct? Looks to me like it shouldn't work.

Reply
 
Thread Tools
post #1 of 51 (permalink) Old 10-23-2017, 11:46 AM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,832
Rep: 18 (Unique: 17)
First let me say that I'm a complete and utter newb to programming, so your answers will have to be very basic. I've decided to learn python. So I'm doing all the free exorcises over at code academy. I'm still near the very beginning, only on lesson 2. And to me, it looks like to get the answer to the total cost of the meal, you'd print this:


meal = meal + tax + tip


But no, as you can see, it wanted this:





How could you add the cost of 2 meals together, and ever get the right answer? The correct answer is clearly saying "meal + meal". And meal is clearly defined in the pic, so they're adding the cost of a whole other meal. I don't get it.

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
Sponsored Links
Advertisement
 
post #2 of 51 (permalink) Old 10-23-2017, 05:52 PM
New to Overclock.net
 
ltpdttcdft's Avatar
 
Join Date: Jun 2013
Posts: 575
Rep: 35 (Unique: 30)
Order of operations.

In this case,

meal = 44.50 + 44.50 * 0.0675
meal = 44.50 + 3.00375
meal = 47.50375

I'm not familiar with Python but more likely than not it follows standard mathematical order of operations:
Parentheses, Exponent, Multiplication, Division, Addition, Subtraction
in that order.

A common way to help remember this is:
Please Excuse My Dear Aunt Sally



ltpdttcdft is offline  
post #3 of 51 (permalink) Old 10-23-2017, 06:28 PM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,832
Rep: 18 (Unique: 17)
I just can't see how its applying the order of operations to this. Probably because I'm used to all my math being splayed out on paper so that I can follow along with relative ease. How is that still following order of operations? The way it looks to me, I never laid out a problem for it that would result in the order of operations even being used.

The way you wrote it out:

meal = 44.50 + 44.50 * 0.0675
meal = 44.50 + 3.00375
meal = 47.50375


Looks to me like it would be ok because the meal variable is being reassigned at the end. I learned that it doesn't care if you change it later, and it will just use the latest definition. So if I said:

meal = $1

and then on the next line I said:

meal = $2

It would forever just use $2 for meal instead of $1.

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
Sponsored Links
Advertisement
 
post #4 of 51 (permalink) Old 10-23-2017, 07:26 PM
New to Overclock.net
 
ltpdttcdft's Avatar
 
Join Date: Jun 2013
Posts: 575
Rep: 35 (Unique: 30)
To be clear, I was evaluating the expression, not writing another script. Basically the "show your work" for a math problem. (Nothing wrong with using paper, I find that it helps me a lot if I write things down on paper first, maybe outline what I'm trying to do in flowcharts/pseudocode before jumping in and coding.)

The stuff on the right of the equal sign is all treated as numbers first, and the expression is evaluated.
Then the result is assigned to the variable to the left of the equals sign.

Be careful with dollar signs, many programming languages treat it as a special character for different things.



ltpdttcdft is offline  
post #5 of 51 (permalink) Old 10-24-2017, 09:40 AM
Retired Staff
 
herkalurk's Avatar
 
Join Date: May 2010
Location: Portland, OR
Posts: 2,039
Rep: 69 (Unique: 58)
Quote:
Originally Posted by ltpdttcdft View Post

Be careful with dollar signs, many programming languages treat it as a special character for different things.

Not to mention the regex. There are specific modules in most languages to deal with currency.



herkalurk is offline  
post #6 of 51 (permalink) Old 10-24-2017, 11:19 AM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,832
Rep: 18 (Unique: 17)
I went back and redid the exorcise a few times and I'm afraid this is still beyond me. Order of operations is no big thing for me, I understand that just fine. What I am missing is why it would bother to do it. Is there another way to code that, which would result in the correct answer, that would not use order of operations? I'm trying to understand why it would be reliant upon something that to me, just doesn't seem necessary.

Maybe I should just move on, and it will all eventually make sense later. I can see where order of operations would be a real big deal in programming, but I thought it would be clear and make sense at any given time, as to why order of operations was being put to use.

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
post #7 of 51 (permalink) Old 10-24-2017, 11:39 AM
New to Overclock.net
 
Join Date: May 2010
Location: Scotland
Posts: 2,365
Rep: 85 (Unique: 67)
Quote:
Originally Posted by PhotonFanatic View Post

I went back and redid the exorcise a few times ...

How's the patient? popesmiley.png

lachen.gif

Sorry, had to redface.gif

Originally Posted by Puck

Apparently it takes 100-something chickens to the face to kill a Communist North Korean.
Spacedinvader is offline  
post #8 of 51 (permalink) Old 10-24-2017, 11:53 AM
Retired Staff
 
herkalurk's Avatar
 
Join Date: May 2010
Location: Portland, OR
Posts: 2,039
Rep: 69 (Unique: 58)
Order of operations means you do less lines of code.
Code:
meal = 44.5
tax = 6.75/100
mealtax = meal*tax
mealtotal=meal+mealtax

The above is the same result, more lines of code. Order of operations for math is a constant. It is the same regardless of language so they use it to make the code simpler. Without it code becomes longer and more drawn out, which isn't needed. I know it doesn't seem useful in 7 lines of code, but imagine 7000. If this were code on a website like this one with hundreds of hits per minute, reducing the amount of code to run, or amount of JS to transfer to the client by every byte helps.



herkalurk is offline  
post #9 of 51 (permalink) Old 10-24-2017, 12:16 PM
New to Overclock.net
 
czin125's Avatar
 
Join Date: Jul 2007
Posts: 2,296
Rep: 52 (Unique: 47)
The original had line 5 as tip but line 7's equation doesn't include tip?

czin125 is offline  
post #10 of 51 (permalink) Old 10-25-2017, 11:49 AM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,832
Rep: 18 (Unique: 17)
Yeah that was my fault I guess. It goes on to add in the tip in later exercises. I just stopped there before moving on, so that I could try to get a handle on the aspect that I didn't understand. Its all making sense now. Someone said they do it so that it makes for a lot less code. A smaller program in the end. Thanks for the help and replies all, it is much appreciated. I'm sure I'll be back with more questions later on lol.

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off