[Tutor] Another assert() question

Dick Moores rdm at rcblue.com
Sat Jul 12 23:35:44 CEST 2008

At 01:28 PM 7/12/2008, Michiel Overtoom wrote:
>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.

Given the name of the list variable, and the expression "Used a color twice!",
it doesn't seem necessary. But maybe I'm missing something.

>It all looks a bit convoluted now, and I'm missing the context in which this
>all happens.

Here it is. <http://py77.python.pastebin.com/f342197d>

>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.

I'm glad to know that. Thanks!

>   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.

For this program, I don't care. But I'll work on it.

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

HTML? Please explain.


More information about the Tutor mailing list