syntax error: invalid syntax -Django View - django-views

Below is my hello apps view.py
***def hello(request):
num_visits = request.session.get('num_visits',0)+1
request.session['num_visits'] = num_visits
if num_visits > 4 : del(request.session['num_visits']
return HttpResponse(';view count ='+str(num_visits))***
after compile, check below error appear on shell:
Syntax Error - Invalid Syntax
Can any expert provide some guidance ?

del doesn't take arguments in parenthesis, it's an operator.
Change this line:
del(request.session['num_visits']
to:
del request.session['num_visits']
And it should work.

thank you for the respond, i managed to address the problem, there was a missing ")" at the del line.

Related

SyntaxError: invalid syntax using pipes.quote

Trying to create a shell script on a raspi3 in python to start a webcam. Getting a syntax error when trying to run the script.
Keep in mind I am new to Python but I have tried each individually to see what prints out, only getting this when I combine the script..
from gpiozero import Button
from pipes import quote
import time
import os
print("your script has started")
camOutput = 'output_http.so -w ./www'
camInput = 'input_raspicam.so -hf'
camStart = '/home/pi/projects/mjpg-streamer/mjpg_streamer -o'.format(quote(camOutput)).'-i'.format(quote(camInput))
print("your script is loaded")
stopButton = Button(26) #shutdown
camButton = Button(25) #web cam
ledButton = Button(24) #top led
while True:
if stopButton.is_pressed:
time.sleep(1)
if stopButton.is_pressed:
os.system("shutdown now -h")
time.sleep(1)
camStart = '/home/pi/projects/mjpg-streamer/mjpg_streamer -o'.format(quote(camOutput)).'-i'.format(quote(camInput))
^
SyntaxError: invalid syntax```
In Python, the dot operator is not used to concatenate strings, only to access properties and methods of an object. Thus, putting a string literal after a dot, such as .'-i', is a syntax error.
You probably want to do something like this, using the format method to replace the {} placeholders with the provided values:
camStart = '/..../mjpg_streamer -o {} -i {}'.format(quote(camOutput),quote(camInput))

Unexpected Indentation in pygame

When I run the following game I get an error "Unexpected Indent" but when you look at the code it's perfectly correct.
The error occurs at del evilGuy[-1]. The indentation is correct still I get this error.
EDIT
The code has been changed a bit. Even now the error occurs at : del evilGuy[-1] showing unexpected indentation.
def evilMove(evilGuy):
evilCoords=[]
#deadZones=[]
#Returns either -1, 0 or 1
randomMovex=random.randrange(-1,2)
randomMovey=random.randrange(-1,2)
newCell={'x':evilGuy[0]['x']+randomMovex,'y':evilGuy[0]['y']+randomMovey}
if (newCell['x']<0 or newCell['y']<0 or newCell['x']>cellSize or newCell['y']>display_height/cellSize):
newCell={'x':display_width/(2*cellSize),'y':display_height/(2*cellSize)
del evilGuy[-1]
evilCoords.append(newCell['x'])
evilCoords.append(newCell['x'])
deadZones.append(evilCoords)
evilGuy.insert(0,newCell)
Solved
The error was a missing '}' in the function evilMove.
Solutiuon is give below.
def evilMove(evilGuy):
evilCoords=[]
#deadZones=[]
#Returns either -1, 0 or 1
randomMovex=random.randrange(-1,2)
randomMovey=random.randrange(-1,2)
newCell={'x':evilGuy[0]['x']+randomMovex,'y':evilGuy[0]['y']+randomMovey}
if (newCell['x']<0 or newCell['y']<0 or newCell['x']>cellSize or newCell['y']>display_height/cellSize):
newCell={'x':display_width/(2*cellSize),'y':display_height/(2*cellSize)} # Here It's missing '}'
del evilGuy[-1]
evilCoords.append(newCell['x'])
evilCoords.append(newCell['x'])
deadZones.append(evilCoords)
evilGuy.insert(0,newCell)
This pb exist from the number of white space
in your code delete the space before def, and add three space before ''', your code will be (just copy and paste) :
def evilMove(evilGuy): # here delete spaces
evilCoords=[]
#deadZones=[]
#Returns either -1, 0 or 1
randomMovex=random.randrange(-1,2)
randomMovey=random.randrange(-1,2)
newCell={'x':evilGuy[0]['x']+randomMovex,'y':evilGuy[0]['y']+randomMovey}
''' # here add spaces
if (newCell['x']<0 or newCell['y']<0 or newCell['x']>cellSize or newCell['y']>display_height/cellSize):
newCell={'x':display_width/(2*cellSize),'y':display_height/(2*cellSize)
'''
del evilGuy[-1]
evilGuy.insert(0,newCell)

wolframalpha api syntax error

i am working on 'wolframalpha' api and i am keep getting this error, i tried to search but not getting any working post on this error if you know please help me to fix this error
File "jal.py", line 9
app_id=’PR5756-H3EP749GGH'
^
SyntaxError: invalid syntax
please help; i have to show project tomorrow :(
my code is
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wolframalpha
import sys
app_id=’PR5756-H3EP749GGH'
client = wolframalpha.Client(app_id)
query = ‘ ‘.join(sys.argv[1:])
res = client.query(query)
if len(res.pods) > 0:
texts = “”
pod = res.pods[1]
if pod.text:
texts = pod.text
else:
texts = “I have no answer for that”
# to skip ascii character in case of error
texts = texts.encode(‘ascii’, ‘ignore’)
print texts
else:
print “Sorry, I am not sure.”
You used a backtick (´) instead of a single-quote (').
app_id='PR5756-H3EP749GGH'
Python directly shows you the error.
Also, use an editor with text highlighting.

Python try except else invalid syntax?

So I am trying to set up a small script in Python's IDLE. The IDLE syntax check tells me this code has a syntax error:
from ftplib import FTP
import os
def ftpconnect(address, username, password):
ftp_connection = 0
ftp = FTP(address)
try:
ftp.login(username, password)
print(ftp.getwelcome())
if ftp.getwelcome() == '220 FTP Connected!':
return 1
else:
return 0
print(ftpconnect('10.10.10.xxx', 'xxx', 'xxx'))
The syntax error comes anywhere that I try to get out of the "try" statement, here being the "else:" line. I've looked around and it seems like I have the right syntax...any thoughts?
Thanks! I'm running Python 2, not 3.
In addition to the problem with my syntax (missing except statement entirely), my ftp attempt statement was outside of the try block. Since I was not "try"ing it, it failed anyway.

error bash: syntax error near unexpected token `(' my code is correct

I am trying to implement a hash function and here is my code:
import BitVector
import glob
path = '/home/vguda/Desktop/.txt'
files=glob.glob(path)
hash = BitVector.BitVector(size = 32)
hash.reset(0)
i = 0
for file in files:
bv = BitVector.BitVector( filename = file )
while 1 :
bv1 = bv.read_bits_from_file(8)
if str(bv1) == "":
break
hash[0:8] = bv1 ^ hash[0:8]
hash >> 8
i = i+1
hash_str = ""
hash_str = str( hash )
text_file = open("/home/vguda/Desktop/result.txt ","w")
text_file.write("Hash Code is %s" %hash_str)
text_file.close()
print hash
print (i)
The displayed error is:
"bash: syntax error near unexpected token `('
First, perhaps this happened in copy and paste, but your indenting in your loop is all messed up, I'm not sure which blocks go where.
When you run things in the shell, you need to either tell python to run it:
python myscript.py
Or, put the following line as the first thing in your program to tell bash to run it as a python program:
#!/usr/bin/python
Currently, bash is trying to run your python program as a bash script, and obviously running into syntax errors.