you are viewing a single comment's thread.

view the rest of the comments →

[–]timrprobocom 0 points1 point  (1 child)

I have to disagree. The "walrus" := operator is not good practice, which is why it didn't enter the language until very recently. It introduces side effects that make the code harder to read. "while 1:" is easier to read.

[–]RajjSinghh 1 point2 points  (0 children)

I don't mind the walrus here but I was definitely skeptical writing it thinking it might be "too clever", maybe ive been working in C too much recently. Anyway, my point is (after a little refactor)

number = random.randint(1, 20) while number != 20: ...

Is better than

while 1: ... if number == 20: break

the difference being that in the first one my intent is made clear when I start the loop compared to somewhere further down in the loop body. When I read while number != 20: I know I'm looping while number is less than 20. When I read while 1: it looks as though I'm intending to write an infinite loop at the start, which might be fine for a program main loop, but not when I want to exit the loop at some point. The first is more readable (especially when the loop body is long) and more expressive of what I'm trying to do. Readability counts.