[Tutor] Valid Username Program

Alan Gauld alan.gauld at yahoo.co.uk
Thu Oct 8 20:08:54 EDT 2020


On 08/10/2020 15:44, Aliyan Navaid wrote:
> Since i'm a beginner could you please give examples for the last part ?
> where you list all the possible scenarios.


I'll try...

>> You can use multiple if statements
>> a) if the tests are completely independent(and will always stay that
>> way) or

if size = value1: ...do something
if size == value2: do something else...

These are tests for specific values so it doesn't need an elif,
only one specific test can ever be true.(Assuming value1 does
not equal value2!)

Of course you still have the problem of creating an else

if size not in (value1,value2,......): ...do else...

So every time you add an if test for a new value you need to
add a value to the "else" test....

A different take in the independent tests scenario is where
we check for different types of concept. Consider an order
processing app that has to check an order item:

if order.stock == 0: put_on_wait_list(order)
if order.value > 1000: process(high_value(order))
if order.shipping_Method == DOWNLOAD: copy_to_ftp(order)

Here we are checking multiple things but we want to perform
all checks on all orders, we explicitly don't want to have
an else before each test. So elif would be the wrong thing to use.


>> b) if, in a case like the example above, you actually want multiple
>> cases to trigger.

Lets reconstruct the previous example slightly so that we do want
multiple outputs:

size = int(input("size"))
if size <= 10: print("Too small")
if size > 10: print("Not bad")
if size > 20: print("Reasonable")
if size > 30: print("That's a big one")
if size > 50: print("That's too big!")

Now if we enter a value <= 20 we get one output.

But if we enter 21 up we get multiple outputs
But they are not contradictory. For example 40 yields:

Not bad
Reasonable
That's a big one.

This is rather contrived but if instead of printing a message we
wanted to perform some size specific data processing then it
might be good to perform it in stages after each applicable if test.

But as I said this is a very rare scenario. in most cases you'd just use
elif:

size = int(input("size"))
if size > 50: print("Too big")
elif size > 30: print("That's a big one!")
elif size > 20: print("Normal")
elif size > 10: print("That's small!")
else: print("Too small")


>> The third scenario to use multiple ifs is inside a function
>> where each 'if' triggers a 'return' statement that will exit
>> the function. 

def appraise_size(size):
   if size > 50: return "Too big"
   if size > 30: return "That's a big one!"
   if size > 20: return "Normal"
   if size > 10: return "That's small!"
   return "Too small"

Each return statement stops execution of the rest of the function
so we don't need elif statements. Similarly we don't need else
since the only way to reach the last line if if all the if
tests failed.

This is probably the most common case where elif is not used
but could have been.


I hope that clarifies what I meant.

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