Overclock.net - An Overclocking Community

Overclock.net - An Overclocking Community (https://www.overclock.net/forum/)
-   Coding and Programming (https://www.overclock.net/forum/142-coding-programming/)
-   -   Why is this correct? Looks to me like it shouldn't work. (https://www.overclock.net/forum/142-coding-programming/1640593-why-correct-looks-me-like-shouldnt-work.html)

PhotonFanatic 10-23-2017 11:46 AM

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.

ltpdttcdft 10-23-2017 05:52 PM

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

PhotonFanatic 10-23-2017 06:28 PM

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.

ltpdttcdft 10-23-2017 07:26 PM

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.

herkalurk 10-24-2017 09:40 AM

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.

PhotonFanatic 10-24-2017 11:19 AM

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.

Spacedinvader 10-24-2017 11:39 AM

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

herkalurk 10-24-2017 11:53 AM

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.

czin125 10-24-2017 12:16 PM

The original had line 5 as tip but line 7's equation doesn't include tip?

PhotonFanatic 10-25-2017 11:49 AM

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.


All times are GMT -7. The time now is 02:11 AM.

Powered by vBulletin® Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.

User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
vBulletin Security provided by vBSecurity (Pro) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.

vBulletin Optimisation provided by vB Optimise (Pro) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.