• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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: 2949
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: 28319
210
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.
 
Sheriff
Posts: 4641
582
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  
 
He's giving us the slip! Quick! Grab this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic