943,782 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 351
  • Python RSS
May 17th, 2009
0

elif error, i really have no clue where this is coming from

Expand Post »
It keeps giving me this elif syntax error in line 103 and I have NO idea where it's coming from. UGH!!!

python Syntax (Toggle Plain Text)
  1. #!/usr/bin/python
  2.  
  3. import os.path
  4.  
  5. #nxn board, in this case n = 10
  6. n = 10
  7. #will hold the positions similar to n-queens
  8. allPositions = []
  9. #current state of board used for comparisons
  10. state = []
  11. #defining captured and inaRow
  12. blackPairCap = 0
  13. whitePairCap = 0
  14. blackInaRow = 0
  15. whiteInaRow = 0
  16.  
  17. #the initial state of the board is all blank
  18. #im storing each row of the board as a list
  19. #this is pretty much only for displaying the board
  20. row0 = []
  21. row1 = []
  22. row2 = []
  23. row3 = []
  24. row4 = []
  25. row5 = []
  26. row6 = []
  27. row7 = []
  28. row8 = []
  29. row9 = []
  30.  
  31. #similar to the n-queens problem,
  32. #i'll make an allDirections list so i can test later for things such as
  33. #if it's next to another piece of itself or the opponent
  34. allDirections = [[-1, 0],[1, 0],[0, -1],[0, 1],[-1, -1],[-1, 1],[1, -1],[1, 1]]
  35.  
  36. #creating all rules without precond or anything
  37. def createPositions():
  38. for i in range(0,n):
  39. for j in range(0,n):
  40. allPositions.append([i,j])
  41.  
  42. #winner
  43. def winner():
  44. if (blackPairCap == 5):
  45. print "White is the winner!"
  46. return True
  47. elif (whitePairCap == 5):
  48. print "Black is the winner!"
  49. return True
  50. elif (blackInaRow >= 5):
  51. print "Black is the winner!"
  52. return True
  53. elif (whiteInaRow >= 5):
  54. print "White is the winner!"
  55. return True
  56. else:
  57. inGameMenu()
  58.  
  59. #for next function
  60. player1 = ""
  61. player2 = ""
  62. comp1 = ""
  63. comp2 = ""
  64. #current players
  65. currentPlayers = []
  66.  
  67. #save files list
  68. FILES = []
  69.  
  70. #startGameMenu
  71. def startGameMenu():
  72. print "Do you have a save file that you would like to run the game from?\n"
  73. saveFile = raw_input("y or n")
  74. saveFile = str(saveFile)
  75. if saveFile == "y":
  76. fileName = raw_input("What is the name of the save file (with extension)? \n")
  77. FILES.append(fileName)
  78. f = open(fileName, 'r')
  79. for line in f:
  80. if line == 0:
  81. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  82. if line == 1:
  83. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  84. if line == 2:
  85. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  86. if line == 3:
  87. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  88. if line == 4:
  89. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  90. if line == 5:
  91. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  92. if line == 6:
  93. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  94. if line == 7:
  95. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  96. if line == 8:
  97. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  98. if line == 9:
  99. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  100. f.close()
  101. inGameMenu()
  102.  
  103. elif saveFile == "n":
  104. print "Welcome to Pente!\n"
  105. print "Would you like to play a new game?\n"
  106. answer = raw_input("y or n:")
  107. answer = str(answer)
  108. if (answer == "y"):
  109. numPlayers = raw_input("How many players are going to be playing? (1-4):\n")
  110. numPlayers = int(numPlayers)
  111. for p in range(0,numPlayers):
  112. print "Would you like to be Player 1, Player 2, PC 1, or PC 2?\n"
  113. player = raw_input("p1, p2, pc1, or pc2:")
  114. player = str(player)
  115. if (player == "p1"):
  116. currentPlayers.append(player1)
  117. elif (player == "p2"):
  118. currentPlayers.append(player2)
  119. elif (player == "pc1"):
  120. currentPlayers.append(comp1)
  121. elif (player == "pc2"):
  122. currentPlayers.append(comp2)
  123.  
  124. print "Which color would you like to play as?\n"
  125. color = raw_input("b (black) or w (white):")
  126. color = str(color)
  127. if (color == "b"):
  128. currentPlayers[p] = "black"
  129. elif (color == "w"):
  130. currentPlayers[p] = "white"
  131.  
  132. inGameMenu()
  133.  
  134. #defining players and their colors
  135.  
  136. #inGameMenu
  137. def inGameMenu():
  138. return
  139.  
  140. #inGameMenu from save file
  141. def saveFileGame():
  142. return
  143.  
  144. #startGameMenu()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
May 17th, 2009
0

Re: elif error, i really have no clue where this is coming from

elif and else must have the same indentation than the corresponding if .
Edit: sorry, in your code, the 2 lines before the elif should be indented like the for statement above.
Last edited by Gribouillis; May 17th, 2009 at 4:36 pm.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
May 17th, 2009
0

Re: elif error, i really have no clue where this is coming from

Ok I restructured things around a little bit and that first elif error went away but now I have another one!!!

python Syntax (Toggle Plain Text)
  1. #!/usr/bin/python
  2.  
  3. import os.path
  4.  
  5. #nxn board, in this case n = 10
  6. n = 10
  7. #will hold the positions similar to n-queens
  8. allPositions = []
  9. #current state of board used for comparisons
  10. state = []
  11. #defining captured and inaRow
  12. blackPairCap = 0
  13. whitePairCap = 0
  14. blackInaRow = 0
  15. whiteInaRow = 0
  16.  
  17. #the initial state of the board is all blank
  18. #im storing each row of the board as a list
  19. #this is pretty much only for displaying the board
  20. row0 = []
  21. row1 = []
  22. row2 = []
  23. row3 = []
  24. row4 = []
  25. row5 = []
  26. row6 = []
  27. row7 = []
  28. row8 = []
  29. row9 = []
  30.  
  31. #similar to the n-queens problem,
  32. #i'll make an allDirections list so i can test later for things such as
  33. #if it's next to another piece of itself or the opponent
  34. allDirections = [[-1, 0],[1, 0],[0, -1],[0, 1],[-1, -1],[-1, 1],[1, -1],[1, 1]]
  35.  
  36. #creating all rules without precond or anything
  37. def createPositions():
  38. for i in range(0,n):
  39. for j in range(0,n):
  40. allPositions.append([i,j])
  41.  
  42. #winner
  43. def winner():
  44. if (blackPairCap == 5):
  45. print "White is the winner!"
  46. return True
  47. elif (whitePairCap == 5):
  48. print "Black is the winner!"
  49. return True
  50. elif (blackInaRow >= 5):
  51. print "Black is the winner!"
  52. return True
  53. elif (whiteInaRow >= 5):
  54. print "White is the winner!"
  55. return True
  56. else:
  57. inGameMenu()
  58.  
  59. #for next function
  60. player1 = ""
  61. player2 = ""
  62. comp1 = ""
  63. comp2 = ""
  64. #current players
  65. currentPlayers = []
  66.  
  67. #save files list
  68. FILES = []
  69.  
  70. #startGameMenu
  71. def startGameMenu():
  72. print "Do you have a save file that you would like to run the game from?\n"
  73. saveFile = raw_input("y or n")
  74. saveFile = str(saveFile)
  75. if saveFile == "y":
  76. saveFileGame()
  77. elif saveFile == "n":
  78. newGame()
  79.  
  80. #defining players and their colors
  81.  
  82. #inGameMenu
  83. def inGameMenu():
  84. return
  85.  
  86. #new game
  87. def newGame():
  88. print "Welcome to Pente!\n"
  89. print "Would you like to play a new game?\n"
  90. answer = raw_input("y or n:")
  91. answer = str(answer)
  92. if (answer == "y"):
  93. numPlayers = raw_input("How many players are going to be playing? (1-4):\n")
  94. numPlayers = int(numPlayers)
  95. for p in range(0,numPlayers):
  96. print "Would you like to be Player 1, Player 2, PC 1, or PC 2?\n"
  97. player = raw_input("p1, p2, pc1, or pc2:")
  98. player = str(player)
  99. if (player == "p1"):
  100. currentPlayers.append(player1)
  101. elif (player == "p2"):
  102. currentPlayers.append(player2)
  103. elif (player == "pc1"):
  104. currentPlayers.append(comp1)
  105. elif (player == "pc2"):
  106. currentPlayers.append(comp2)
  107. print "Which color would you like to play as?\n"
  108. color = raw_input("b (black) or w (white):")
  109. color = str(color)
  110. if (color == "b"):
  111. currentPlayers[p] = "black"
  112. elif (color == "w"):
  113. currentPlayers[p] = "white"
  114. inGameMenu()
  115.  
  116. #inGameMenu from save file
  117. def saveFileGame():
  118. fileName = raw_input("What is the name of the save file (with extension)? \n")
  119. FILES.append(fileName)
  120. f = open(fileName, 'r')
  121. for line in f:
  122. if line == 0:
  123. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  124. if line == 1:
  125. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  126. if line == 2:
  127. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  128. if line == 3:
  129. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  130. if line == 4:
  131. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  132. if line == 5:
  133. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  134. if line == 6:
  135. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  136. if line == 7:
  137. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  138. if line == 8:
  139. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  140. if line == 9:
  141. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  142. f.close()
  143. inGameMenu()
  144.  
  145. #startGameMenu()

This one is in line 101
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
May 17th, 2009
0

Re: elif error, i really have no clue where this is coming from

lines 101 - 106 need to lose a tab, i.e., un-indent each of those lines once.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
May 17th, 2009
0

Re: elif error, i really have no clue where this is coming from

I did that and now there's another error in line 102 (lines switched around cuz I kinda reformatted the whole thing). Ugh I hate these elif errors!

python Syntax (Toggle Plain Text)
  1. #!/usr/bin/python
  2.  
  3.  
  4.  
  5. import os.path
  6.  
  7.  
  8.  
  9. #nxn board, in this case n = 10
  10.  
  11. n = 10
  12.  
  13. #will hold the positions similar to n-queens
  14.  
  15. allPositions = []
  16.  
  17. #current state of board used for comparisons
  18.  
  19. state = []
  20.  
  21. #defining captured and inaRow
  22.  
  23. blackPairCap = 0
  24.  
  25. whitePairCap = 0
  26.  
  27. blackInaRow = 0
  28.  
  29. whiteInaRow = 0
  30.  
  31.  
  32.  
  33. #the initial state of the board is all blank
  34.  
  35. #im storing each row of the board as a list
  36.  
  37. #this is pretty much only for displaying the board
  38.  
  39. row0 = []
  40.  
  41. row1 = []
  42.  
  43. row2 = []
  44.  
  45. row3 = []
  46.  
  47. row4 = []
  48.  
  49. row5 = []
  50.  
  51. row6 = []
  52.  
  53. row7 = []
  54.  
  55. row8 = []
  56.  
  57. row9 = []
  58.  
  59.  
  60.  
  61. #similar to the n-queens problem,
  62.  
  63. #i'll make an allDirections list so i can test later for things such as
  64.  
  65. #if it's next to another piece of itself or the opponent
  66.  
  67. allDirections = [[-1, 0],[1, 0],[0, -1],[0, 1],[-1, -1],[-1, 1],[1, -1],[1, 1]]
  68.  
  69.  
  70.  
  71. #creating all rules without precond or anything
  72.  
  73. def createPositions():
  74.  
  75. for i in range(0,n):
  76.  
  77. for j in range(0,n):
  78.  
  79. allPositions.append([i,j])
  80.  
  81.  
  82.  
  83. #winner
  84.  
  85. def winner():
  86.  
  87. if (blackPairCap == 5):
  88.  
  89. print "White is the winner!"
  90.  
  91. return True
  92.  
  93. elif (whitePairCap == 5):
  94.  
  95. print "Black is the winner!"
  96.  
  97. return True
  98.  
  99. elif (blackInaRow >= 5):
  100.  
  101. print "Black is the winner!"
  102.  
  103. return True
  104.  
  105. elif (whiteInaRow >= 5):
  106.  
  107. print "White is the winner!"
  108.  
  109. return True
  110.  
  111. else:
  112.  
  113. inGameMenu()
  114.  
  115.  
  116.  
  117. #for next function
  118.  
  119. player1 = ""
  120.  
  121. player2 = ""
  122.  
  123. comp1 = ""
  124.  
  125. comp2 = ""
  126.  
  127. #current players
  128.  
  129. currentPlayers = []
  130.  
  131.  
  132.  
  133. #save files list
  134.  
  135. FILES = []
  136.  
  137.  
  138.  
  139. #startGameMenu
  140.  
  141. def startGameMenu():
  142.  
  143. print "Do you have a save file that you would like to run the game from?\n"
  144.  
  145. saveFile = raw_input("y or n")
  146.  
  147. saveFile = str(saveFile)
  148.  
  149. if saveFile == "y":
  150.  
  151. fileName = raw_input("What is the name of the save file (with extension)? \n")
  152.  
  153. FILES.append(fileName)
  154.  
  155. f = open(fileName, 'r')
  156.  
  157. for line in f:
  158.  
  159. if line == 0:
  160.  
  161. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  162.  
  163. if line == 1:
  164.  
  165. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  166.  
  167. if line == 2:
  168.  
  169. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  170.  
  171. if line == 3:
  172.  
  173. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  174.  
  175. if line == 4:
  176.  
  177. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  178.  
  179. if line == 5:
  180.  
  181. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  182.  
  183. if line == 6:
  184.  
  185. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  186.  
  187. if line == 7:
  188.  
  189. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  190.  
  191. if line == 8:
  192.  
  193. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  194.  
  195. if line == 9:
  196.  
  197. row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
  198.  
  199. f.close()
  200.  
  201. inGameMenu()
  202.  
  203. elif saveFile == "n":
  204.  
  205. newGame()
  206.  
  207.  
  208.  
  209. #defining players and their colors
  210.  
  211.  
  212.  
  213. #inGameMenu
  214.  
  215. def inGameMenu():
  216.  
  217. return
  218.  
  219.  
  220.  
  221. #inGameMenu from save file
  222.  
  223. def saveFileGame():
  224.  
  225. return
  226.  
  227.  
  228.  
  229. #newGame
  230.  
  231. def newGame():
  232.  
  233. print "Welcome to Pente!\n"
  234.  
  235. print "Would you like to play a new game?\n"
  236.  
  237. answer = raw_input("y or n:")
  238.  
  239. answer = str(answer)
  240.  
  241. if (answer == "y"):
  242.  
  243. numPlayers = raw_input("How many players are going to be playing? (1-4):\n")
  244.  
  245. numPlayers = int(numPlayers)
  246.  
  247. for p in range(0,numPlayers):
  248.  
  249. print "Would you like to be Player 1, Player 2, PC 1, or PC 2?\n"
  250.  
  251. player = raw_input("p1, p2, pc1, or pc2:")
  252.  
  253. player = str(player)
  254.  
  255. if (player == "p1"):
  256.  
  257. currentPlayers.append(player1)
  258.  
  259. elif (player == "p2"):
  260.  
  261. currentPlayers.append(player2)
  262.  
  263. elif (player == "pc1"):
  264.  
  265. currentPlayers.append(comp1)
  266.  
  267. elif (player == "pc2"):
  268.  
  269. currentPlayers.append(comp2)
  270.  
  271.  
  272.  
  273. print "Which color would you like to play as?\n"
  274.  
  275. color = raw_input("b (black) or w (white):")
  276.  
  277. color = str(color)
  278.  
  279. if (color == "b"):
  280.  
  281. currentPlayers[p] = "black"
  282.  
  283. elif (color == "w"):
  284.  
  285. currentPlayers[p] = "white"
  286.  
  287. inGameMenu()
  288.  
  289.  
  290.  
  291. #startGameMenu()

don't mind the double spacing, idk why it did that, it's not like that in my editor
Last edited by gotm; May 17th, 2009 at 9:06 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
May 17th, 2009
0

Re: elif error, i really have no clue where this is coming from

Quote ...
Ugh I hate these elif errors!
Then don't use it. Some examples that also simplify the code.
Python Syntax (Toggle Plain Text)
  1. ## instead of if line == 0 ,1 2, etc.
  2. for line in f:
  3. if (line > -1) and (line < 10):
  4. row0.extend([line[x] for x in range (0, 10)])
  5. ##
  6. ##------------------------------------------------------------------
  7. """
  8. #the initial state of the board is all blank
  9. #im storing each row of the board as a list
  10. #this is pretty much only for displaying the board
  11.  
  12. row0 = []
  13. row1 = []
  14. row2 = []
  15. row3 = []
  16. row4 = []
  17. row5 = []
  18. row6 = []
  19. row7 = []
  20. row8 = []
  21. row9 = []
  22. """
  23. ## use a list of lists or a dictionary instead
  24. ## this is a dictionary with the key = 0 --> 9 instead
  25. ## of row0 --> row9
  26. row_dic = {}
  27. for j in range(0, 10):
  28. row_dic[j] = []
  29. ##
  30. ##------------------------------------------------------------------
  31. """ instead of
  32. elif (player == "p2"):, etc.
  33. """
  34. player_dic = {}
  35. player_dic["p1"] = player1
  36. player_dic["p2"] = player2
  37. player_dic["pc1"] = comp1
  38. player_dic["pc2"] = comp2
  39. for p in range(0,numPlayers):
  40.  
  41. print "Would you like to be Player 1, Player 2, PC 1, or PC 2?\n"
  42. player = raw_input("p1, p2, pc1, or pc2:")
  43. player = player.lower()
  44. if (player in player_dic):
  45. currentPlayers.append( player_dic[player] )
  46. else:
  47. print "That is not a valid player"
Hopefully you get the idea.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: CGI versus CherryPy ?
Next Thread in Python Forum Timeline: how to avoid spaces in login and password fields





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC