madscheme / introducing-python

An introduction to Python
763 stars 418 forks source link

'diff =' vs 'diff :=' #16

Open vzhgit opened 1 week ago

vzhgit commented 1 week ago

Hello. I'm reading a traslated version of the book so I can't say on which page is following example but it contains two errors:

>>> tweet_limit = 280
>>> tweet_string = "Blah" * 50
>>> if diff := tweet_limit - len(tweet_string) >= 0;
...     print("A fitting tweet")
... else:
...     print("Went over by", abs(diff))
...
A fitting tweet

First error is invalid syntax: ';' Second error about logic: in your exanple diff variable has boolean type so print("Went over by", abs(diff)) will always print 0 instead of number of characters. The correct version of condition:

if (diff := tweet_limit - len(tweet_string)) >= 0:

https://stackoverflow.com/a/63876683/6188044

madscheme commented 6 days ago

That looks like an error in translation. The printed and online versions contain a colon, not a semicolon.

Thanks for pointing this out!