Overclock.net banner

1 - 20 of 51 Posts

·
Registered
Joined
·
1,992 Posts
Discussion Starter #1
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:<br><br><br>
meal = meal + tax + tip<br><br><br>
But no, as you can see, it wanted this:<br><br><br><a class="H-lightbox-open" href="http://www.overclock.net/content/type/61/id/3139182/"><img alt="" class="lightbox-enabled" data-id="3139182" data-type="61" src="http://www.overclock.net/content/type/61/id/3139182/flags/LL" style="; width: 1629px; height: 843px"></a><br><br><br>
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.
 

·
Registered
Joined
·
634 Posts
Order of operations.<br><br>
In this case,<br><br>
meal = 44.50 + 44.50 * 0.0675<br>
meal = 44.50 + 3.00375<br>
meal = 47.50375<br><br>
I'm not familiar with Python but more likely than not it follows standard mathematical order of operations:<br>
Parentheses, Exponent, Multiplication, Division, Addition, Subtraction<br>
in that order.<br><br>
A common way to help remember this is:<br>
Please Excuse My Dear Aunt Sally
 

·
Registered
Joined
·
1,992 Posts
Discussion Starter #3
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.<br><br>
The way you wrote it out:<br><br>
meal = 44.50 + 44.50 * 0.0675<br>
meal = 44.50 + 3.00375<br>
meal = 47.50375<br><br><br>
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:<br><br>
meal = $1<br><br>
and then on the next line I said:<br><br>
meal = $2<br><br>
It would forever just use $2 for meal instead of $1.
 

·
Registered
Joined
·
634 Posts
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.)<br><br>
The stuff on the right of the equal sign is all treated as numbers first, and the expression is evaluated.<br>
Then the result is assigned to the variable to the left of the equals sign.<br><br>
Be careful with dollar signs, many programming languages treat it as a special character for different things.
 

·
Registered
Joined
·
2,039 Posts
<div class="quote-container" data-huddler-embed="/t/1640593/why-is-this-correct-looks-to-me-like-it-shouldnt-work/0_20#post_26409792" data-huddler-embed-placeholder="false"><span>Quote:</span>
<div class="quote-block">Originally Posted by <strong>ltpdttcdft</strong> <a href="/t/1640593/why-is-this-correct-looks-to-me-like-it-shouldnt-work/0_20#post_26409792"><img alt="View Post" class="inlineimg" src="/img/forum/go_quote.gif"></a><br><br>
Be careful with dollar signs, many programming languages treat it as a special character for different things.</div>
</div>
<br>
Not to mention the regex. There are specific modules in most languages to deal with currency.
 

·
Registered
Joined
·
1,992 Posts
Discussion Starter #6
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.<br><br>
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.
 

·
Registered
Joined
·
2,365 Posts
<div class="quote-container" data-huddler-embed="/t/1640593/why-is-this-correct-looks-to-me-like-it-shouldnt-work/0_100#post_26410905" data-huddler-embed-placeholder="false"><span>Quote:</span>
<div class="quote-block">Originally Posted by <strong>PhotonFanatic</strong> <a href="/t/1640593/why-is-this-correct-looks-to-me-like-it-shouldnt-work/0_100#post_26410905"><img alt="View Post" class="inlineimg" src="/img/forum/go_quote.gif"></a><br><br>
I went back and redid the exorcise a few times ...</div>
</div>
<br>
How's the patient? <img alt="popesmiley.png" class="bbcode_smiley" src="http://files.overclock.net/images/smilies/popesmiley.png"><br><br><img alt="lachen.gif" class="bbcode_smiley" src="http://files.overclock.net/images/smilies/lachen.gif"><br><br>
Sorry, had to <img alt="redface.gif" class="bbcode_smiley" src="http://files.overclock.net/images/smilies/redface.gif">
 

·
Registered
Joined
·
2,039 Posts
Order of operations means you do less lines of code.<br><div class="bbcode_code">
<div class="bbcode_code_head">Code:</div>
<pre>
<code>meal = 44.5
tax = 6.75/100
mealtax = meal*tax
mealtotal=meal+mealtax</code>
</pre></div>
<br>
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.
 

·
Registered
Joined
·
1,992 Posts
Discussion Starter #10
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.
 

·
Registered
Joined
·
2,299 Posts
Is there any difference if you did either one of these?<br><br>
meal = 44.50<br>
tax = meal*0.0675<br>
tip = meal*0.1500<br>
total = meal + tax + tip<br><br>
or<br><br>
meal = 44.50<br>
tax = 0.0675<br>
tip = 0.1500<br>
total = meal + meal * ( tax + tip ) // 0.0675+0.1500 = 0.2175
 

·
Registered
Joined
·
1,992 Posts
Discussion Starter #12
Yeah it finally threw an error when I tried several other variations. But I think its possible that it was because it wasn't what the lesson wanted. I'm seeing that some things that will work, don't really go along with their lesson plans. It would work for a bit, then throw an error. You'd probably have to do the exercises to see what I'm talking about, but sometimes it will let you, and sometimes it just says its the wrong answer and you don't progress.
 

·
Registered
Joined
·
1,992 Posts
Discussion Starter #13
I'm back. So how is 14 correct in this instance? Clearly there are 13 characters in "Norwegian Blue". Yet as you can see, it spits out 14. Is it counting the space between the n, and the B?<br><br><a class="H-lightbox-open" href="http://www.overclock.net/content/type/61/id/3142752/"><img alt="" class="lightbox-enabled" data-id="3142752" data-type="61" src="http://www.overclock.net/content/type/61/id/3142752/flags/LL" style="; width: 1060px; height: 616px"></a>
 

·
Registered
Joined
·
58 Posts
Yes whitespace is a character in a string in python therefore it counts it.
 

·
Registered
Joined
·
1,992 Posts
Discussion Starter #15
So any blank space is automatically whitespace? What about completely empty lines? Makes sense that they would be, but I know its best to ask before assuming anything lol
 

·
 
Joined
·
29,532 Posts
<div class="quote-container" data-huddler-embed="/t/1640593/why-is-this-correct-looks-to-me-like-it-shouldnt-work/0_100#post_26421647" data-huddler-embed-placeholder="false"><span>Quote:</span>
<div class="quote-block">Originally Posted by <strong>PhotonFanatic</strong> <a href="/t/1640593/why-is-this-correct-looks-to-me-like-it-shouldnt-work/0_100#post_26421647"><img alt="View Post" class="inlineimg" src="/img/forum/go_quote.gif"></a><br><br>
So any blank space is automatically whitespace? What about completely empty lines? Makes sense that they would be, but I know its best to ask before assuming anything lol</div>
</div>
<br>
Typically a newline counts as 1 character, but can also count as 2 in some cases: <a href="https://en.wikipedia.org/wiki/Newline#Representations" target="_blank">https://en.wikipedia.org/wiki/Newline#Representations</a> - only the first point is relevant here.
 

·
Registered
Joined
·
2,312 Posts
With math and order of opperations stuff.... its just easier to just use a lot of parentheses for readability sake. c= a + (B*2) Total = Meal + (Meal * tip) Granted this can be simplified in this case by Total = Meal * Tip+1 but thats just harder to understand whats happening.<br><br>
DO NOT WORRY ABOUT OPTIMIZED CODE. Nothing needs to be optimized until it actually becomes a problem. I have wasted a ton of time on projects because i was trying to optimize my code early on making it harder to read and the optimizations are very negligible 99% of the time. When you need to optimize , its more likely that you need better logic instead of these little things. SO yeah... make things easy and readable first. If need be, clean it up later.<br><br>
With counts and stuff, its weird and you need to know which way its going about it. If you do a length, it could start with 0 instead of 1. So, x= [1,2,3] gives x.length = 2. while x.count() = 3 . As they said, spaces and newlines count. I doubt you need to worry about this, but encoding can also come into play where you have single and double byte characters.... not sure what that would effect, but it is something.
 

·
 
Joined
·
29,532 Posts
<div class="quote-container" data-huddler-embed="/t/1640593/why-is-this-correct-looks-to-me-like-it-shouldnt-work/0_100#post_26433459" data-huddler-embed-placeholder="false"><span>Quote:</span>
<div class="quote-block">Originally Posted by <strong>Mrzev</strong> <a href="/t/1640593/why-is-this-correct-looks-to-me-like-it-shouldnt-work/0_100#post_26433459"><img alt="View Post" class="inlineimg" src="/img/forum/go_quote.gif"></a><br><br>
I doubt you need to worry about this, but encoding can also come into play where you have single and double byte characters.... not sure what that would effect, but it is something.</div>
</div>
<br>
Emojis come to mind. They will register as 2 characters, flag emojis as 4:<br><br><img alt="6EUvDAD.png" class="bbcode_img" src="https://i.imgur.com/6EUvDAD.png">
 

·
Registered
Joined
·
1,992 Posts
Discussion Starter #19
<div class="quote-container" data-huddler-embed="/t/1640593/why-is-this-correct-looks-to-me-like-it-shouldnt-work/10#post_26433459" data-huddler-embed-placeholder="false"><span>Quote:</span>
<div class="quote-block">Originally Posted by <strong>Mrzev</strong> <a href="/t/1640593/why-is-this-correct-looks-to-me-like-it-shouldnt-work/10#post_26433459"><img alt="View Post" class="inlineimg" src="/img/forum/go_quote.gif"></a><br><br>
DO NOT WORRY ABOUT OPTIMIZED CODE. Nothing needs to be optimized until it actually becomes a problem.</div>
</div>
<br>
Believe it or not that actually takes a load off my mind, even as a newbie to programming. I was always thinking about how it could be done better, if that was even important, etc. I was wondering just how "strict" it was going to be about having everything perfect.<br><br>
Next problem. Well its not really a problem, but to me its something that doesn't make sense. Here is part of a python exorcise from codeacademy:<br><br><a class="H-lightbox-open" href="http://www.overclock.net/content/type/61/id/3150158/"><img alt="" class="lightbox-enabled" data-id="3150158" data-type="61" src="http://www.overclock.net/content/type/61/id/3150158/flags/LL" style="; width: 561px; height: 128px"></a><br><br>
The answer was True. But how can that be true, when you have "Is greater than OR = to". Its the or that I have a problem with. Or = to? No. It is not = to -4. But if you say >=, you're saying it could be. But it never could be, because 40 ** 2 can never be = to -4. So how can you say that its True?
 

·
 
Joined
·
29,532 Posts
<div class="quote-container" data-huddler-embed="/t/1640593/why-is-this-correct-looks-to-me-like-it-shouldnt-work/0_100#post_26444304" data-huddler-embed-placeholder="false"><span>Quote:</span>
<div class="quote-block">Originally Posted by <strong>PhotonFanatic</strong> <a href="/t/1640593/why-is-this-correct-looks-to-me-like-it-shouldnt-work/0_100#post_26444304"><img alt="View Post" class="inlineimg" src="/img/forum/go_quote.gif"></a><br><br><a class="H-lightbox-open" href="http://www.overclock.net/content/type/61/id/3150158/"><img alt="" class="lightbox-enabled" data-id="3150158" data-type="61" src="http://www.overclock.net/content/type/61/id/3150158/flags/LL" style="; width: 561px; height: 128px"></a><br><br>
The answer was True. But how can that be true, when you have "Is greater than OR = to". Its the or that I have a problem with. Or = to? No. It is not = to -4. But if you say >=, you're saying it could be. But it never could be, because 40 ** 2 can never be = to -4. So how can you say that its True?</div>
</div>
<br><br>
>= means greater than or equal to. So when you write that line, you imply 'is (40 times 4) greater than or equal to (-4)?' in which case it's true. If you remove the equals-to sign you should still get true.
 
1 - 20 of 51 Posts
Top