Invalid Syntax?
Posted: Tue Mar 25, 2014 10:45 pm
by Mikachu
So I was coding a tic-tac-toe game in python, via a book: "Raspbery Pi Projects"
When I had finished, I ran it and it responded with "invalid syntax"
It highlighted the ' at the end of 'Tic-Tac-Toe'
Halp
Re: Invalid Syntax?
Posted: Wed Mar 26, 2014 6:49 am
by elParaguayo
We can't guess what the problem is.
You need to post your code (inside code tags please) and the error message.
(Actually, my guess would be a missing bracket somewhere but I'd need to see your code to check!)
Re: Invalid Syntax?
Posted: Wed Mar 26, 2014 12:54 pm
by DougieLawson
http://eu.wiley.com/WileyCDA/WileyTitle ... 55430.html
Has a downloadable version. Grab a copy of that and use the diff command to compare against your broken version.
Re: Invalid Syntax?
Posted: Wed Mar 26, 2014 2:14 pm
by Mikachu
the ' at the end of 'Tic-Tac-Toe' is highlighted
#!/usr/bin/env python
# Tic-Tac-Toe 5 - 2 player game
board = [ '1', '2', '3', '4', '5', '6', '7', '8', '9' ]
wins = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]
def play() :
printBoard()
print 'Tic-Tac-Toe'
print 'two players'
while True :
wipeBoard()
player_turn = 'X'
while checkWin(swapPlayer(player_turn)) == False and canMove() == True :
getMove(player_turn)
printBoard()
player_turn = swapPlayer(player_turn)
if checkWin(swapPlayer(player_turn)):
print 'Player',swapPlayer(player_turn),'wins ... New Game'
else:
print 'A draw. ... New game'
def swapPlayer(player):
if player == 'X' :
player = 'O'
else:
player = 'X'
return player
def getMove(player):
global board
correct_number = False
while correct_number == False :
square = raw_input('Square to place the '+ player + ' ')
try:
square = int(square)
except:
square = -2
square -= 1 # make input number match internal representation
if square >= 0 and square < 10 : # number in range
if board[square] == ' ' : # if it is blank
board[square] = player
correct_number = True
else:
print 'Square already occupied'
else :
print 'incorrect square try again'
def wipeBoard() :
global board
for square in range(0, len(board) ) :
board[square] = ' '
def printBoard():
print
print '|',
for square in range(0,9):
print board[square],'|',
if square == 2 or square == 5 :
print
print '- - - - - - -'
print '|',
print
print
def canMove(): # see if move can be made
move = False
for square in range(0, len(board) ) :
if board[square] == ' ':
move = True
return move
def checkWin(player):
win = False
for test in wins :
count = 0
for squares in test :
if board[squares] == player :
count +=1
if count == 3 :
win = True
return win
if __name__ == '__main__':
play()
Re: Invalid Syntax?
Posted: Wed Mar 26, 2014 3:58 pm
by elParaguayo
Please put your code in code tags ie.
[/code] as we've lost all your indentations.
I think you'll get a syntax error on a print statement if python 3 receives python 2 code. i.e. python 3 would expect:
I don't use python 3 so I can't say if this will work, but you could try changing your first line to:
If you type "python" at the command line, which version number does it show?
Re: Invalid Syntax?
Posted: Sun Apr 15, 2018 4:09 pm
by Emin
hi i have a problem can you please help
os.system(&quot;fswebcam -r 960x720 -d /dev/video0 /home/pi/webcam.jpg&quot;)
^
SyntaxError: invalid syntax
it sees "&" this as a invalid syntax what should i do to fix it ? here is my full code
# -*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import time
import os
buttonPin = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonPin,GPIO.IN)
while True:
if (GPIO.input(buttonPin)):
os.system(&quot;fswebcam -r 960x720 -d /dev/video0 /home/pi/webcam.jpg&quot;)
os.system(&quot;python /home/pi/mailgonder.py&quot;)
Re: Invalid Syntax?
Posted: Sun Apr 15, 2018 4:36 pm
by DougieLawson
Emin wrote: ↑Sun Apr 15, 2018 4:09 pm
Code: Select all
os.system(&quot;fswebcam -r 960x720 -d /dev/video0 /home/pi/webcam.jpg&quot;)
You've got some HTML artifacts in there, because of the way you've copied the code from a web page.
Change to
Code: Select all
os.system("fswebcam -r 960x720 -d /dev/video0 /home/pi/webcam.jpg")
Re: Invalid Syntax?
Posted: Sun Apr 15, 2018 6:08 pm
by B.Goode
In addition to what @Dougie said, the 4-year old topic that you have chosen to add your query to previously ended with sensible advice to use the forum's Code tag mark-up to maintain the indented structure of your Python script.