Mikachu
Posts: 2
Joined: Tue Mar 25, 2014 10:38 pm

Invalid Syntax?

Tue Mar 25, 2014 10:45 pm

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

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Invalid Syntax?

Wed Mar 26, 2014 6:49 am

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!)
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Invalid Syntax?

Wed Mar 26, 2014 12:54 pm

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Mikachu
Posts: 2
Joined: Tue Mar 25, 2014 10:38 pm

Re: Invalid Syntax?

Wed Mar 26, 2014 2:14 pm

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()

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Invalid Syntax?

Wed Mar 26, 2014 3:58 pm

Please put your code in code tags ie.

Code: Select all

[code]your code here
[/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:

Code: Select all

print("Tic-Tac-Toe")
I don't use python 3 so I can't say if this will work, but you could try changing your first line to:

Code: Select all

#!/bin/env python2.7
If you type "python" at the command line, which version number does it show?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Emin
Posts: 1
Joined: Sun Apr 15, 2018 3:53 pm

Re: Invalid Syntax?

Sun Apr 15, 2018 4:09 pm

hi i have a problem can you please help

os.system(&amp;quot;fswebcam -r 960x720 -d /dev/video0 /home/pi/webcam.jpg&amp;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(&amp;quot;fswebcam -r 960x720 -d /dev/video0 /home/pi/webcam.jpg&amp;quot;)

os.system(&amp;quot;python /home/pi/mailgonder.py&amp;quot;)

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Invalid Syntax?

Sun Apr 15, 2018 4:36 pm

Emin wrote:
Sun Apr 15, 2018 4:09 pm

Code: Select all

os.system(&amp;quot;fswebcam -r 960x720 -d /dev/video0 /home/pi/webcam.jpg&amp;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")
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Invalid Syntax?

Sun Apr 15, 2018 6:08 pm

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.

Return to “Python”