Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

python codes

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone help me please to understand this lines if ever someone has knowledge to these. I dont know what is the meaning of "Places" and the number 2 inside the parentheses.

#Read Temperature sensor
def ReadTemperature(places):
    humidity, temperature = Adafruit_DHT.read_retry(sensor,pin)
    temperature = round(humidity,places)
    return temperature


while True:
#take Temperature Reading via DHT22
    temperature_percentage = ReadTemperature(2)
 
Ranch Hand
Posts: 2808
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a variable that you have created and passing to the function.
 
Saloon Keeper
Posts: 27273
193
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing that you're using Circuit Python. I moved this question to the Arduino forum, although it's possible that you're using a Raspberry Pi. I just happen to use the DHT units on Arduinos - although in Arduino's C++ like language, not Python.

The round() function takes a numeric value (humidity) and rounds it to a specified number of decimal places. And actually, that code is defective, because it's treating temperature and humidity as the same thing. In this case, since you passed the value 2 to ReadTemperature, it rounds the humidity (!!!) to 2 decimals, then returns it as a "temperature".

The proper code should be like this:


I don't know what generally-accepted name formation rules are for Python, but I always use either C-like (read_temperature) or Java-like (readTemperature). Beginning a name with a capital letter is usually something only done for classnames and constant names (which are generally all upper-case).

And the code tags goe around the formatted text. That's why my code is in a box, but yours has an empty box and no pretty code. Check Use Code Tags for details.
 
Marshal
Posts: 4349
559
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Manubay wrote:I dont know what is the meaning of "Places" ...


Places specifies the number of devcimal places to use with the Python round function

Michael Manubay wrote:... and the number 2 inside the parentheses.


It most likely means than there are multiple DHT sensors, and the number 2 specifies to read from the the senor on GPIO pin #2.


Edit: looks like I didn't read the question properly and I didn't notice Tim's response. Ugh  
 
Good night. Drive safely. Here's a tiny ad for the road:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic