r/itsaunixsystem • u/AnAngryBanker • Oct 14 '24
[Nightsleeper] Some horrible, unindented python code to slow down a train
- no indentation
- multiple nested try blocks with seemingly no except
- stray " in the middle of that string
- input's argument should be something written to the prompt
- even if it wasn't you'll struggle to turn that into an int
- you probably want to be passing in that speed variable somewhere, right?
+1 for sanitising the speed I guess, wouldn't want the train to start moving backwards.
504
Upvotes
View all comments
Show parent comment
1
u/aezart Oct 16 '24
No, it doesn't. Python is the only language I'm aware of where this works (although I wouldn't be surprised if it worked in something like MATLAB).
In Java (tested with openjdk 17.0.1 2021-10-19 LTS, could be different in more modern versions), it's a compile-time error:
In C,
trueis just1andfalseis just0, and so it collapses to either1 <= 100or0 <= 100, which are always true. Thus it gives you incorrect results for out-of-range values.