Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization 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
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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: 2424
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: 23441
159
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: 3460
493
Android 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  
 
Crusading Chameleon likes the size of this ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic