The if statement


if ( logical-expression ) :

    action1

else :

    action2

where logical-expression is a test expression that evalutes to either True or False. Both action1 and action2 must be non-empty statement lists. The actions are indented one-level further than the start of the if statement. The else is indented at the same level as the if. Both if and else are keywords

reply = input( 'Enter one or two words: ' )

list = reply.split()

n = len( list )

if ( n == 1 ) :

    # list size is 1

    print( 'one word' )

else :

    # otherwise, list size is 2

    print( 'two words' )

By getting the input and turning it into a list, we can easily determine the number of words entered (i.e., the length of the list). By determining whether the list size is one. We can produce the appropriate response.

reply = input( 'Enter one or two words: ' )

list = reply.split()

n = len( list )

# assume they followed directions

response = 'You followed directions'

if ( ( n != 0 ) and ( n != 1 ) ) : # what does it take to be wrong? n is neither 0 nor 1

    # they did not, so response needs updating

    response = 'You did not follow directions'

print( response)

To correctly determine whether the input is too short or too long, the if test expression has multiple terms that are anded-ed together. My advice always parenthesize each term in a logical expression. Although the problem is artificial and can be done other ways. It shows what was intended.

reply = input( 'Enter one or two words: ' )

list = reply.split()

n = len( list )

# assume they followed directions

response = 'You followed directions'

if ( ( n != 0 ) and ( n != 1 ) ) : # what does it take to be wrong? n is neither 0 nor 1

    # they did not, so response needs updating

    response = 'You did not follow directions'

else :

    # no action necessary

    pass

print( response)

reply = input( 'Enter one or two words: ' )

list = reply.split()

n = len( list )    

# what are the possibilities for the user response

# enters no words

# enters one word

# enters two words

# enters more than two words (i.e., it is not of the prior cases)    

response = '' # none so far    

if ( n == 0 ) :

    # they did not follow directions -- no words were supplied

    response = 'You did not enter any words'

elif ( n == 1 ) :

    # they did follow directions by entering one word

    response = 'Thanks for following directions by entering one word'

elif ( n == 2 ) :

    # they did follow directions by entering two words

    response = 'Thanks for following directions by entering two words'

else :

    # they did not follow directions -- too many words supplied

    response = 'You entered too many words'    

print( response)

The above if statement recognizes there are four possibilities of concern when users react to the prompt: they did not enter any words, they entered one word, they entered two words, or they did something else (i.e., entered more than two words). For each of these possibilities, the if statement takes the possibility into account and correctly responds.

 


 

 

 
   
   
   
 
  © 2019 Jim Cohoon   Resources from previous semesters are available.