Page 1 of 1

Syntax Error in Script

Posted: Tue Apr 18, 2017 6:02 pm
by CPaolo
Hello all, i'm new to this wonderful world and i own a RPI2 B with the DHT11 sensor. I'm using a script in python for uploading the Temperature online.
From yesterday after the reboot (after apt-get update and upgrade) i get a syntax error in this line:

print "Temp={0:f}*C Humidity={1:f}%".format(temperature, humidity)

between " and .

But i did not change the script. What should be happen ?
Can someone help me ?

Thankyou all
CPaolo

Re: Syntax Error in Script

Posted: Tue Apr 18, 2017 6:23 pm
by gordon77
Can you upload all your code to here, or provide a link to where you got it?

Re: Syntax Error in Script

Posted: Tue Apr 18, 2017 6:40 pm
by DougieLawson
Is it python2 or python3 code? Are you trying to run python2 code using python3?

Re: Syntax Error in Script

Posted: Tue Apr 18, 2017 7:03 pm
by CPaolo
This is the code........
I don't know if the python version is changed ......


#!#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

############################################################
# This code uses the Beebotte API, you must have an account.
# You can register here: http://beebotte.com/register
############################################################

import time
import Adafruit_DHT
from beebotte import *

### Replace API_KEY and SECRET_KEY with those of your account
bbt = BBT("00000000000000000000000", "00000000000000000000000000000000000000")

period = 300 ## Sensor data reporting period (1 minute)
pin = 4 ## Assuming the DHT11 sensor is connected to GPIO pin number 4

### Change channel name and resource names as suits you
temp_resource = Resource(bbt, 'PI2B', 'temperatura')
humid_resource = Resource(bbt, 'PI2B', 'umidita')
now = time.strftime("%c")

def run():
while True:
### Assume
humidity, temperature = Adafruit_DHT.read_retry( Adafruit_DHT.DHT11, pin )
if humidity is not None and temperature is not None:
print "Temp={0:f}*C Humidity={1:f}%".format(temperature, humidity)
print time.strftime("%c")
try:
#Send temperature to Beebotte
temp_resource.write(temperature)
#Send humidity to Beebotte
humid_resource.write(humidity)
except Exception:
## Process exception here
print "Error while writing to Beebotte"
else:
print "Failed to get reading. Try again!"

#Sleep some time
time.sleep( period )

run()





Thanks
CPaolo

Re: Syntax Error in Script

Posted: Tue Apr 18, 2017 7:05 pm
by DougieLawson
That's a python2 program.

Re: Syntax Error in Script

Posted: Tue Apr 18, 2017 7:07 pm
by CPaolo
Hello, Python version is still 2.7

CPaolo

Re: Syntax Error in Script

Posted: Tue Apr 18, 2017 9:41 pm
by stderr
CPaolo wrote:Hello, Python version is still 2.7
When you type python at the terminal, what do you get, 2.7.x? On arch, I get 3.6.0 On my pi3, I do get 2.7.9. If your program needs 2.7, trusting that just looking in the path for "python" is version 2.7 might be risky. Perhaps a shebang that asks for a specific at least major version of python would be better.

Re: Syntax Error in Script

Posted: Wed Apr 19, 2017 5:32 pm
by CPaolo
yes, 2.7

If i restore a backup SD the script works .......

CPaolo

Re: Syntax Error in Script

Posted: Wed Apr 19, 2017 7:18 pm
by stderr
CPaolo wrote:yes, 2.7
Did you change the shebang in your program to python 2.7

#!/usr/bin/python2.7

after verifying that it is there by running at the command line:

/usr/bin/python2.7

The error that you get seems to occur for me in the interpreter when I copy your line of code in and change the variables to sensible integer constants when I'm in arch running python 3.6.0 and it does not occur in raspbian under 2.7.9 .

Re: Syntax Error in Script

Posted: Fri Apr 21, 2017 7:16 pm
by CPaolo
I don't understand ......