[Tutor] (no subject)
Alan Gauld
alan.gauld at yahoo.co.uk
Sun Oct 18 07:34:11 EDT 2020
On 18/10/2020 08:58, Aliyan Navaid wrote:
> I want to write a program which list ALL the errors while entering a user
> name.
>
> (If I use if elif blocks the program will end at the first error it
> catches)
You have just answered your own question.
Stop using elif clauses. Just use if.
hasErrors = False
if characters < 3:
print(“Username too short”)
hasErrors = True
if characters > 50:
print(“Username too long”)
hasErrors = Trueif special_characters:
print(“Special characters not allowed”)
hasErrors = True
if not hasErrors:
print(“Username is valid”)
if hasErrors: # Now do something to recover!
But it must be said that its an unusual strategy.
Most programs detect the first error and stop.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list