Question

I want to input a string of five numbers with spaces between them and use raw_input() for it. However the second item(the portion that's between the first and second spaces) is claimed to be a syntax error. Code below:

#class for final output - used as an ad hoc static string
class StatString:
        outstring = ""

#function to check if Boom or Trach or both
def BoomTrach(num,B,T):
        Boom = False
        Trach = False
        temp = num
        while temp != 0:
                if num % B == 0:
                        Boom == True
                        break
                if (temp % 10) % B == 0:
                        Boom = True
                        break
                temp = (temp - temp % 10) / 10
                temp = num
        while temp != 0:
                if num % T == 0:
                        Trach = True
                        break
                if (temp % 10) % T == 0:
                        Trach = True
                        break
                temp = (temp - temp % 10) / 10
        if Boom and Trach:
                herestring.outstring = herestring.outstring + "Boom-Trach"
        elif Boom:
                herestring.outstring = herestring.outstring + "Boom"
        elif Trach:
                herestring.outstring = herestring.outstring + "Trach"
        else:
                herestring.outstring = herestring.outstring + str(num)


#start of "main" portion
def main():
        inS = raw_input()  <<<--- Input here
        arr = inS.split(' ')
        X = int(arr[0])
        Y = int(arr[1])
        CountFrom = int(arr[2])
        jump = int(arr[3])
        CountUntil = int(arr[4])

        #variable for error check
        error = False

        #checking for errors
        if X < 1 or X > 9 or Y < 1 or Y > 9:
                print "X and Y must be between 1 and 9"
                error = True

        if jump == 0:
                print "jump cannot be 0"
                error = True

        elif (CountUntil - CountFrom) % jump != 0:
                print "cannot jump from %d to %d",CountFrom,CountUntil
                error = True

        if error:
                exit()

        if CountFrom < 0 and CountUntil < 0 and jump > 0:
                jump = jump * (-1)

        herestring = StatString()
        while CountFrom != CountUntil:
                BoomTrach(CountFrom,X,Y)
                CountFrom = CountFrom + jump
                if(CountFrom != CountUntil):
                        herestring.outstring = herestring.outstring + ","
        print herestring.outstring

error message: (the second 1 was marked as the source of the error)

>>> 1 1 1 1 1
SyntaxError: invalid syntax
>>> 
Was it helpful?

Solution

I know what happened. You just run this module, and you thought that you start running it from the main function (like in C for example). There is no problem with the raw_input line (the first line of the input). The problem is that your module does not try to read anything! You just typed "1 1 1 1 1" which is of course syntax error...

Append to your code this line to run the main function:

main()

You can also write the code of the main function not in some function, to get the same effect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top