My Kingdom for a Pair of Parens

For want of a Parens

 Well, turns out the Dunning Kruger force is strong in
this one (me).

I thought I was advancing past noob status. I thought I had a modicum of competence in Python. Not so.

Oh why couldn't the PyCharm interpreter just scream at me, "Fool, you forget the Open/Close Parentheses plus colon at the end of the string method!"
No. I had to spend hours staring at the code but not seeing it. Finally, the aha moment came. I slapped palm into forehead.

Enough said about my failings of the day. Now it is time for us to don our bathing suits and jump into the code swamp provided below.

By the way. You can't simply paste your code into Blogger. HTML will eliminate the indentations unless we use the <pre> and <code> opening tags plus their partner closers, </code> </pre>. 
See the How To Article Here

The goal was to put together a simple module of test functions that query a user's input() to see if it is valid. Validity tests are useful for all programs where a user might make a mistake when prompted for an input string. So it makes sense to create my own module for testing validity.

The below code includes some test runs of the draft qif_025(arg1) function. The latter is supposed to verify that the user input a string argument in the range of '0' to '5' and nothing else. The input reponse can be used to control how many filled stars out of 5 to give to a polling question.


   
   ## My Query If Functions  #<-- the "My" makes it clear it's not outside module

# One assumption is that all arg's come from a user input() result
#   --and thus it's of type string
#       -- but that assumption does not necessarily hold true
#           -- I'n yet ready to try the 'try' statement and its exceptions

def qif_025(arg1): #return True is arge1 is a string expressing an integer in the range 0 to 5
    if not isinstance(arg1, str):  #<-- True if the arg is NOT of type string
                # see https://www.geeksforgeeks.org/python-isinstance-method/
        print ("Type of arg is ", type(arg1))
        return False
    elif len(arg1) != 1:            #<-- True if length is NOT one character
        print('Length of below arg is not one')
        return False # since length of arg string is not 1, we have error
    elif not arg1.isnumeric():   #<-- forgot the parens !!!
                # see https://www.geeksforgeeks.org/python-string-isnumeric-method/
        print("Below arg is not numeric")
        return False  # arg string is not all digits (0-9_)
    elif int(arg1) not in range(0,6):
        print ('Below arg is not 0 to 5')
        return False
    else:
        return True

# Testing the qif_025 function using single integer inputs
for i in range(-2,8):     #we will loop through a test range
    arg1 = str(i)
    valid = qif_025(arg1)
    if valid:
        result = "Good"
        print(i, f'This input is {result}')
    else:
        result = "Bad input"
        print(i, f'This input is {result}')
# end of test #1

# Testing the qif_025 function using non-numeric user inputs
for s in ['no', 'hello', '25', '0', '5', '6', 'y']:
    valid = qif_025(s)
    if valid:
        result = "Good"
        print(s, f'This input is {result}')
    else:
        result = "Bad "
        print(s, f'This input is {result}')
# end of test #2

    


When the above code is executed, I get the folowing output. (p.s. Please excuse the spelling errors.)


    
    
Length of below arg is not one
-2 This input is Bad input
Length of below arg is not one
-1 This input is Bad input
0 This input is Good
1 This input is Good
2 This input is Good
3 This input is Good
4 This input is Good
5 This input is Good
Below arg is not 0 to 5
6 This input is Bad input
Below arg is not 0 to 5
7 This input is Bad input
Length of below arg is not one
no This input is Bad 
Length of below arg is not one
hello This input is Bad 
Length of below arg is not one
25 This input is Bad 
0 This input is Good
5 This input is Good
Below arg is not 0 to 5
6 This input is Bad 
Below arg is not numeric
y This input is Bad 

Process finished with exit code 0
    

Comments

Popular posts from this blog

Git-ten Up to Speed

True Confessions

Obsidian is Not King of the Hill