[Tutor] A Better Way to Write This?
18 Feb 2001 09:57:51 +0800
"Britt Green" <email@example.com> wrote:
> if choice == ("black") or choice == ("white") or choice ==
> ("green") or choice == ("blue"):
> return 1
> return 0
> While this works, it seems ugly. What would happen, for example, if
> there were ten or twenty correct colors?
> So I'm wondering if there is a better way to write this?
return choice in ['black', 'white', 'green', 'blue']
Because this list may be used somewhere else, it would be better
to put it in a variable so that when you add a color, you won't
have to mess with parts of the code that use it.
acceptable_colors = ['black', 'white', 'green', 'blue']
return choice in acceptable_colors
Georges Ko (Taipei, Taiwan) 2001-02-18 firstname.lastname@example.org / ICQ: 8719684