• @conditional_soup@lemm.ee
    link
    fedilink
    6
    edit-2
    2 years ago
    def count_fingers(hand: list): 
        count = len(hand)
        if count != 5:
            if count < 5:
                raise Exception("Check if fingers missing, or just smart ass")
            else:
                raise Exception("Oh... oh no.")
        return count
    
    • Hurglet
      link
      fedilink
      52 years ago

      You can do

      if (count := len(hand)) != 5:
          # do something with "count"
      

      Btw, looks much nicer

        • Hurglet
          link
          fedilink
          72 years ago

          It’s not the ternary operator, it is the walrus operator introduced in Python 3.8 if I’m correct

          • @conditional_soup@lemm.ee
            link
            fedilink
            52 years ago

            You are correct. I came back to say that I’ll use the walrus operator when it’s pushed into my cold, dead hands, but… I might actually use it, now that I’ve refreshed myself on it.