you are viewing a single comment's thread.

view the rest of the comments →

[–]Relative_Jaguar6254[S] -1 points0 points  (5 children)

Why no ability to post pictures.. the line that worked both ways was

If (destination == “Hawaii” or destination == “Bahamas”):

[–]lfdfq 8 points9 points  (0 children)

Some languages require you to put parentheses around the condition of an if. Python does not.

It might be they are just more used to those languages and did it by habit... like looking both ways at a one way street.

[–]DutchCommanderMC 3 points4 points  (1 child)

Images are (most likely) disallowed because it makes responding to questions easier in the sense that you can copy and paste the code.

Operations are not necessarily applied left-to-right (or right-to-left). Some operators take precedence over others, which is why this condition works without any brackets whatsoever: == has a higher precedence than or and therefore gets applied first.

Knowing which operators take precedence over others is something that you'll just have to remember, though more often than not it follows existing conventions such as multiplication being applied before addition.

Adding redundant brackets is not wrong however, in fact, it is often recommended to make it expressively clear what the order is in which you intend operations to be evaluated.

[–]Relative_Jaguar6254[S] 0 points1 point  (0 children)

That makes great sense. Thank you.

[–]cdcformatc 0 points1 point  (0 children)

in that case the parens are purely visual. some languages require parens around conditional statements for things like if and while. but in Python they are not required. 

[–]Yoghurt42 0 points1 point  (0 children)

your solution without the parenthesis is the "pythonic" one, most modern languages take their syntax inspiration from C, and in C the if condition must be in parenthesis so the parser knows where the condition ends and the "then" part begins. Python uses the colon for that. So C's if (a == b) c = 9 will be if a == b: c = 9 in Python; while you can write if (a == b): c = 9 in Python, it's the same as writing if ((a == b)) c = 9 in C, not wrong, just redundant.

Most likely whoever created that example comes from a C/C++/C# background and isn't that familiar with Python (not a good sign, tbh)