Syntax Error CGI script - python

name = form.getvalue('name')
age = form.getvalue('age') + 1
next_age1 = int(form["age"].value
print "Content-type: text/html"
print
print "<html><head>"
print "<p> Hello, %s</p>" % (name)
print "<p> Next year, you will be %s years old.</p>" % next_age1
Having trouble understanding why this doesn't work.
Can someone please help me?
Thank you!

You are missing a closing parenthesis:
next_age1 = int(form["age"].value
# ----^ -------^ nothing here
Rule of thumb: when you get a syntax error you cannot immediately spot on that line, look at the previous line to see if you balanced your parenthesis and braces properly.

Related

End of statement expected while using Dot Format

I need to print the following of code :
print('hello "my friend '{}' "'.format(name))
such that the output is:
'hello "my friend 'ABCD' " '
But I get the error: End of Statement expected
What is the correct syntax?
You need to escape quotes if you want to use them in a string:
print('hello "my friend \'{}\'"'.format(name))
Try this,
print("""hello "my friend '{}' " """.format(name))

Trying to send a message with Yowsup Python

Hello there im trying to send a message with yowsup but i have not been successful can you please help me im getting IndentationError: unexpected indent Thank you
from yowsup.layers.interface import YowInterfaceLayer, ProtocolEntityCallback
from yowsup.layers.protocol_messages.protocolentities import TextMessageProtocolEntity
from yowsup.common.tools import Jid
class EchoLayer(YowInterfaceLayer):
#ProtocolEntityCallback("message")
def onMessage(self, messageProtocolEntity):
if messageProtocolEntity.getType() == 'text':
self.onTextMessage(messageProtocolEntity)
reply = 1
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
self.toLower(messageProtocolEntity.forward(messageProtocolEntity.getFrom()))
self.toLower(messageProtocolEntity.ack())
self.toLower(messageProtocolEntity.ack(True))
#ProtocolEntityCallback("receipt")
def onReceipt(self, entity):
self.toLower(entity.ack())
def onTextMessage(self,messageProtocolEntity):
# just print info
print("Echoing %s to %s" % (messageProtocolEntity.getBody(), messageProtocolEntity.getFrom(False)))
IndentationError: unexpected indent
This is a common kind of problem every pythoner has hit with. your code has some extra space or any tab is used.so basically you have to check every line and manually remove the excess gaps(shortcuts) and replace them with spaces insted.
The Unexpected indent error message will always tell you what line the problem is detected on.
In your case, it's obviously due to the fact that the if and reply lines don't match up.

Why is is this "Invalid syntax" in Python?

This isn't my code, it is a module I found on the internet which performs (or is supposed to perform) the task I want.
print '{'
for page in range (1,4):
rand = random.random()
id = str(long( rand*1000000000000000000 ))
query_params = { 'q':'a',
'include_entities':'true', 'lang':'en',
'show_user':'true',
'rpp': '100', 'page': page,
'result_type': 'mixed',
'max_id':id}
r = requests.get('http://search.twitter.com/search.json',
params=query_params)
tweets = json.loads(r.text)['results']
for tweet in tweets:
if tweet.get('text') :
print tweet
print '}'
print
The Python shell seems to indicate that the error is one Line 1. I know very little Python so have no idea why it isn't working.
This snippet is written for Python 2.x, but in Python 3.x (where print is now a proper function). Replace print SomeExp with print(SomeExpr) to solve this.
Here's a detailed description of this difference (along with other changes in 3.x).

if statement invalid syntax

I'm trying to execute the following code, but I'm getting
SyntaxError: invalid syntax
at the if statement. I'm sure it's related to html as it worked previously without the preceding html; I'm having some difficulty understanding how to go in and out with html and Python as you can see.
class MainPage(webapp.RequestHandler):
def get(self):
#preceding ("""<html><head> tags...
</head>
<body>""")
self.response.out.write(today.strftime("""<html><body><p style='color:#3E3535'>%A, %d %B</p>""")
if mon <= 3:
var1 = "winter"
Thanks in advance for suggestions; the if/elifs are indented properly on page.
If this is your code, then you're missing a close-paren to close out your last function call.
Change it to the following:
self.response.out.write(today.strftime("""<html><body><p style='color:#3E3535'>%A, %d %B</p>"""))

String formatting: string specifier in a string constant

Is there a way to insert a string in a string constant/variable that contains a string specifier?
Example:
temp_body = 'Hello %s, please visit %s to confirm your registration.'
body = temp_body % (name, url)
But this raises a TypeError.
Usually it is the way strings are generated e.g. msg template will be loaded from db or some file and things inserted in between, what is url and name in your case?
This works on my machine
>>> temp_body = 'Hello %s, please visit %s to confirm your registration.'
>>> temp_body%("anurag", "stackoverflow")
'Hello anurag, please visit stackoverflow to confirm your registration.'
Also try if str(name), str(url) works , ost probably it won't and try to fix that problem instead.
Works on my machine(TM).
Are you sure that name and url really are strings? What do you get when you do
>>> type(name), type(url)

Categories

Resources