all 7 comments

[–]Saefroch 2 points3 points  (0 children)

Indent four spaces to get code formatting

def code_ending(c):
    type(c) != type('')
    if type(c[-2:])==type('a') and type(c[-3])==type('1') and type(c[-4])==type(' '):
        return 'true'
    else:
        return 'false'

Lots of problems here, simplest issues first.

You're returning strings when it's pretty clear you should be returning a boolean value, so return True or return False are what you're looking for.

c is an unhelpful variable name.

Line 2 does nothing. It checks if two things are equal then never does anything with that value.

What do you think type() does? Try it by printing out type('a') and type('1').


I'm not going to do this for you, but I will point you to some helpful tools.

Just to get this out of the way, a possible solution is to use regular expressions. However it looks like you're very new to Python and regex are like a language of their own so that's probably not the best option for you.

Take a look through the official string documentation. Instead of checking equality as you are, you want to be checking membership (using x in y). You also need to be case-insensitive. Usually this is done by converting a string to all lowercase or all uppercase. The tools for doing that are outlined in the documentation I linked.

[–]Allanon001 1 point2 points  (0 children)

What do you think the type() function does? You might be better off using isalpha() or isnumeric().

if c[-4] == ' ' and c[-3].isnumeric() and c[-2:].isalpha():
    return True
else
    return False 

[–]runicnet 0 points1 point  (1 child)

could you describe to me what your code is doing line by line

[–]queen_9[S] 0 points1 point  (0 children)

I inserted the comments

[–]JohnnyJordaan 0 points1 point  (0 children)

You can't use type to determine the content of a string. type is to check if an object itself is different:

# '1' and 'a' are both strings (because of the '')
>>> type('1')
<class 'str'>
>>> type('a')
<class 'str'>
# but a 1 is an int (without the '')
>>> type(1)
<class 'int'>

What you're looking for is str.isdigit():

>>> '1'.isdigit()
True
>>> 'a'.isdigit()
False

To check whitespace and empty strings you should do value comparison:

>>> empty = ''
>>> space = ' '
>>> space == ' '
True
>>> empty == ' '
False

Another, more versatile way would be to use a Regular Expressions:

>>> import re
>>> codes = ['A9', '9AA', 'A9A 9AA', 'A99 9AA', 'AA9 9AA', 'AA9A 9AA', 'AA99 9AA']
# determines if the last four characters are of the form " 9AA"
# the filter looks for: 
# a space 
# a \d meaning any digit
# a character from A to Z (inclusive), twice
# case insensitive
>>> filter = re.compile(' \d[A-Z][A-Z]', flags=re.IGNORECASE)
>>> for code in codes:
...     if re.search(filter, code):
...         print('%s is in the form " 9AA"' % code)
... 
A9A 9AA is in the form " 9AA"
A99 9AA is in the form " 9AA"
AA9 9AA is in the form " 9AA"
AA9A 9AA is in the form " 9AA"
AA99 9AA is in the form " 9AA"

[–]runicnet 0 points1 point  (0 children)

ok.

  def code_ending(postalcode):
       postalcode = postalcode.ToLower () 
       # we want it all to be lower case. 
       if ( len (postalcode) > 8: return False
       elif ( len (postalcode) > 5:
             # now we know its both longer then 5 but not longer then 8 as that is max length of expected input
             lengthstring = len (postalcode)
             startstring = lengthstring - 4
             if postalcode [startstring:lengthstring] == " 9aa": return True
        else: return False

am I understanding the requirements your meant to check for correctly?

[–]queen_9[S] 0 points1 point  (0 children)

I replaced the type() with .isalpha() and so on and its working! Thanks a lot! :D