[Tutor] (no subject)
Sibylle Koczian
nulla.epistola at web.de
Sun Oct 18 13:42:40 EDT 2020
Am 18.10.2020 um 13:34 schrieb Alan Gauld via Tutor:
> 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.
>
Exception: if two conditions can't both be true, but can both be false,
as the two first conditions here. In this case I'd leave the elif in.
> 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.
>
More information about the Tutor
mailing list