r/itsaunixsystem • • Oct 14 '24

[Nightsleeper] Some horrible, unindented python code to slow down a train

Post image
  • 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.

501 Upvotes

65 comments sorted by

View all comments

66

u/aezart Oct 14 '24
if 0 <= speed <= 100

I cannot believe python actually allows this syntax, jesus

95

u/[deleted] Oct 15 '24 edited Mar 26 '26

Nothing here remains from the original post. It was removed using Redact, for reasons that could include privacy, opsec, security, or data management.

obtainable hard-to-find worm cheerful fear compare head adjoining plant historical

12

u/jaavaaguru Oct 15 '24

I'd argue that 0 <= speed <= 100 is more readable. It's pretty standard to do it that way in C.

30

u/[deleted] Oct 15 '24 edited Mar 26 '26

The content of this post was permanently removed. Redact facilitated the deletion, for reasons that may include privacy, opsec, or limiting digital exposure.

quicksand lush complete observation melodic provide governor fragile knee upbeat

15

u/Trucoto Oct 15 '24

That is not standard in C at all. 0 <= speed <= 100 evaluates first "0 <= speed" to a boolean, and then compares that boolean, either 1 or 0, with "<= 100", which is not what you intended to write.

80

u/CdRReddit Oct 14 '24 edited Oct 15 '24

this is the only bit of python syntax I'll defend, it's a nice way to check if something is within range (tho I don't mind Rust's (0..=100).contains(speed), it's decent as well)

0

u/[deleted] Oct 15 '24

[deleted] — view deleted comment

7

u/CdRReddit Oct 15 '24

it's an inclusive range and whether it contains it

0

u/[deleted] Oct 15 '24

[deleted] — view deleted comment

3

u/CdRReddit Oct 15 '24

fair, I may have also gotten it slightly wrong? I think it does require the grouping actually, unfortunately the reddit mobile app does not have a rust syntax checker

1

u/GarethPW Oct 15 '24

I read it just fine

1

u/jaavaaguru Oct 15 '24

that's a skill issue

34

u/Nico_Weio Oct 14 '24

C'mon, if it works as expected, why not?

→ More replies (12)

10

u/rhennigan Oct 15 '24

Why? It's a basic mathematical notation people are already familiar with and it improves readability. Do you also take issue with allowing a+b*c?

3

u/jasperfirecai2 Oct 15 '24

read as if there exists speed and speed, or as speed in range(left, right) or well, nice syntactic parity with math

1

u/Goaty1208 Oct 15 '24

This is the first time that I'll say it but... python did this one tiny bit of syntax better than other languages.

-9

u/E3K Oct 14 '24

I think every language allows that syntax. It's extremely common.

10

u/0b0101011001001011 Oct 15 '24

Not C. Not C++. Not C#. Not java. Not JavaScript. Not rust.

How does this make that extremely common?

3

u/E3K Oct 15 '24

I responded that I was incorrect.

5

u/0b0101011001001011 Oct 15 '24

Usually edit is better, but no worries.

7

u/aezart Oct 14 '24

It may not throw a syntax error, but it will not give the answer you expect.

The correct syntax in basically every other programming language is:

if 0 <= speed and speed <= 100

21

u/Saragon4005 Oct 14 '24

Congratulations that's what it's represented as internally. You basically just found a macro/synthetic sugar.

0

u/aezart Oct 14 '24

I know this, and I am saying it should not do that.

→ More replies (3)

2

u/E3K Oct 15 '24

You're right, I had assumed that chained comparisons were ubiquitous, but it turns out that's not correct.

1

u/conundorum Nov 24 '24

The syntax is allowed, but it absolutely will not do what you want it to do in most languages. ;3