2013-11-24 77 views
0

我想輸入一個包含空格的五個數字的字符串,並使用raw_input()作爲它。然而,第二項(第一和第二空間之間的部分)聲稱是syntax error。下面的代碼:raw_input()無效語法

#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 

錯誤消息:(第二1被標記爲錯誤的來源)

>>> 1 1 1 1 1 
SyntaxError: invalid syntax 
>>> 
+1

請提供完整的錯誤追溯。 –

回答

0

我知道發生了什麼。你只需運行這個模塊,並且你認爲你從主函數開始運行它(例如在C中)。 raw_input行(輸入的第一行)沒有問題。問題是你的模塊不會嘗試讀取任何東西!你只要輸入「1 1 1 1 1」,這當然是語法錯誤的...

追加到該行代碼運行的主要功能:

的main()

你也可以寫主函數的代碼不在某些函數中,以獲得相同的效果。

+0

顯然這不是所有的錯誤...該死的。 – Sunspawn