[Tutor] Valid Username Program
Richard Damon
Richard at Damon-Family.org
Thu Oct 8 09:05:55 EDT 2020
On 10/8/20 7:29 AM, Aliyan Navaid wrote:
> def valid_username(username):
>
> if len(username) < 3:
>
> print("Username must be atleast 3 characters long")
>
> elif len(username) > 15:
>
> print("Username is too long")
>
> else:
>
> print("Valid Username")
>
>
>
>
>
> Dear Sir,
>
> So this program checks the validity of a username.
>
> I wanted to ask that instead of elif, I could’ve written another If
> statement in this program right ?
>
> And can you please also explain that when to use elif statement and when
> to use multiple if statements.
>
If the elif had been an if, then the final else would be run for any
username <= 15 characters long, even if they are <3 characters long
(those would print:
Username must be at least 3 characters long
Valid Username
The key here is that an if / elif / else chain will only run exactly one
of the following code sections, but with if / if / else the final two
are not eliminated by the success of the first.
--
Richard Damon
More information about the Tutor
mailing list