Skip to content

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

@vzhgit

Description

@vzhgit

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions