TOPCOD3R
TOPCOD3R

Reputation: 154

Errors When Formatting Code In Python

I know this is probably quite a simple problem but I am having an issue with the formatting of my function. I am getting a lot of 'unexpected indent' and also 'unexpected token'. I keep trying to format the function correctly but I have no idea why these errors keep on appearing. Here is my function:

def stringCheck(stringForCheck, letterOrNumber):

    valid = True

    x = 0

    a = int(ord(stringForCheck)

    length = len(stringForCheck)

        if LetterOrNumber == 'Letter':

            lowerBoundary = 65

            upperBoundary = 90

        elif LetterOrNumber == 'Number':

            lowerBoundary = 48

            upperBoundary = 57

        while valid == True and x < length:

            if a < lowerBoundary or a > upperBoundary:

                valid = False

            else:

                valid = True

        x = x + 1

    stringCheck = valid



stringCheck('2','Number')

Upvotes: 0

Views: 107

Answers (2)

Christoph
Christoph

Reputation: 692

  1. Remove the unnecessary blank lines
  2. You are missing a closing bracket here: a = int(ord(stringForCheck)
  3. From the line if LetterOrNumber == 'Letter': to your while loop the lines have one indentation level too much.

After fixing the code it should look something like this:

def stringCheck(stringForCheck, letterOrNumber):
    valid = True
    x = 0
    a = int(ord(stringForCheck))
    length = len(stringForCheck)

    if LetterOrNumber == 'Letter':
        lowerBoundary = 65
        upperBoundary = 90
    elif LetterOrNumber == 'Number':
        lowerBoundary = 48
        upperBoundary = 57

    while valid is True and x < length:
        if a < lowerBoundary or a > upperBoundary:
            valid = False
        else:
            valid = True

    x = x + 1
    stringCheck = valid

stringCheck('2', 'Number')

Upvotes: 3

Sam McCracken
Sam McCracken

Reputation: 1

Try adding a close bracket after the line

    a = int(ord(stringForCheck))

Upvotes: 0

Related Questions