Watch Me Code 1: Yes Or No

Our approach

  1. take input "yes" or "no"
  2. convert to lower case
  3. slice off first character and return it!

In [1]:
dir(str)


Out[1]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmod__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 'isalnum',
 'isalpha',
 'isdecimal',
 'isdigit',
 'isidentifier',
 'islower',
 'isnumeric',
 'isprintable',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'maketrans',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']

In [9]:
help(str.lower)


Help on method_descriptor:

lower(...)
    S.lower() -> str
    
    Return a copy of the string S converted to lowercase.


In [1]:
x = "Nick"
x.lower()


Out[1]:
'nick'

In [11]:
def YesOrNo(text):
    first_char = text[0]
    first_char = first_char.lower()
    if "y" == first_char:
        return True
    elif "n" == first_char:
        return False
    else:
        return None
        
x = input("Please enter y/n, or yes/no")
result = YesOrNo(x)
if result is None:
    print("Please enter a y/n")
elif result:
    print("You entered Yes")
elif not result:
    print("You entered No")


Please enter y/n, or yes/noNo
You entered No

In [ ]:


In [ ]: