[Tutor] Another assert() question

Michiel Overtoom motoom at xs4all.nl
Sat Jul 12 22:28:08 CEST 2008


Dick wrote:

> I was hoping to put some sort of explanation of failure in an 
> assert statement. But how to do it?
> So I'd like to know what that 'expression' in the syntax can be, 
> and how to use it.

I think it would help if you separate the detection of duplicate colors from
the assert statement.
It all looks a bit convoluted now, and I'm missing the context in which this
all happens.
First detect the presence of duplicate colors in a True/False variable, then
use that variable in an assert.

Oh, and by the way, you don't have to convert a set to list to be able to
take it's length.

  colors=["red","blue","green","blue","yellow","blue"]
  duplicatesfound = len(set(colors)) != len(colors)
  assert not duplicatesfound, "A color has been used more than once"

Exercise left for the reader: Report which colors were used more than once.

And do me a favor, post in plain text, not HTML.

Greetings,

-- 
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Vallopillil
http://www.catb.org/~esr/halloween/halloween4.html



More information about the Tutor mailing list