peterb
March 7, 2020, 11:14pm
1
What am I doing wrong!
The first code works
The second throws up the error below:
while True:
temperature = bme280.get_temperature()
pressure = bme280.get_pressure()
humidity = bme280.get_humidity()
print(‘{:05.2f}*C {:05.2f}hPa {:05.2f}%’.format(temperature, pressure, humidity))
time.sleep(1)
while True:
temperature = bme280.get_temperature()
pressure = bme280.get_pressure()
humidity = bme280.get_humidity()
ltr559.update_sensor()
lux = ltr559.get_lux()
print(‘{:05.2f}*C {:05.2f}hPa {:05.2f}% {:06.2f}lux’.format(temperature, pressure, humidity,lux)
time.sleep(1)
File “all-values+.py”, line 74
time.sleep(1)
^
SyntaxError: invalid syntax
Oddly it has worked when previously but now doesn’t
I think there is a missing ) on the line above.
Since indentation is significant in Python it is better to paste code with ``` each side to get a properly formatted code block.
while True:
temperature = bme280.get_temperature()
pressure = bme280.get_pressure()
humidity = bme280.get_humidity()
print(’{:05.2f}*C {:05.2f}hPa {:05.2f}%’.format(temperature, pressure, humidity))
time.sleep(1)
while True:
temperature = bme280.get_temperature()
pressure = bme280.get_pressure()
humidity = bme280.get_humidity()
ltr559.update_sensor()
lux = ltr559.get_lux()
print(’{:05.2f}*C {:05.2f}hPa {:05.2f}% {:06.2f}lux’.format(temperature, pressure, humidity,lux) ) <--
time.sleep(1)
peterb
March 8, 2020, 9:56am
3
Thanks Chris, I definitely hadn’t spotted that missing bracket.
Corrected and ran again and got a new error!
all-values+.py - Read temperature, pressure, lightlevel and humidity
Press Ctrl+C to exit!
21.07*C 691.37hPa 58.40% 006.68lux
Traceback (most recent call last):
File “all-values+.py”, line 74, in
time.sleep(10)
AttributeError: ‘builtin_function_or_method’ object has no attribute ‘sleep’
Which I also don’t understand as it is declared at the beginning
from time import sleep, time, asctime, localtime, strftime, gmtime
If you do
import time
then you import the whole time module as one symbol time
and then you can do time.sleep()
If you do
from time import sleep
Then you import just the symbols listed and you then can do sleep()
I should also explain that the time module has a time function time.time()
which you have imported as time()
, so time
is now a function instead of a module, hence the error.
peterb
March 8, 2020, 2:31pm
6
Thanks again. Sorry if these are noobie type mistakes, as you can tell I am feeling my way.
So my line starting
from time
can be reduced to:
import time
and that will give me all the other functions?
At the moment all I want to do is add a date/timestamp to each line of data in the print statement.
print(’{:05.2f}*C {:05.2f}hPa {:05.2f}% {:06.2f}lux’.format(temperature, pressure, humidity,lux) )
This will then become the csv data line that will be saved.
Yes if you import time
you are importing a module called time and all the functions in it can be accessed as time.name()
.
If you import something from time you are importing a list of functions, which can be accessed without a prefix but no other symbols from time can be used.
You can also do from time import *
in which case you can access all the functions without a prefix but you stand more chance of getting name clashes if you do that.
peterb
March 27, 2020, 11:16am
8
Deleted post.
Posted in Forum in error