Avatar of dynamicOne
dynamicOne

asked on 

Syntax Problem

I have a Python script I am trying to run.

import commands
cmd = 'gpio read 1'
output = commands.getoutput(cmd)

if commands.getoutput(cmd) == 0:
   print ('The door is closed.')

When I run it, nothing happens. It just goes to the next line instead of printing.

Any idea what I am doing wrong?
PythonProgramming Languages-OtherProgramming Theory

Avatar of undefined
Last Comment
TommySzalapski
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

What are you trying to get it to do? commands.getoutput(cmd) is returning a non-zero value. What are you wanting it to return?
Avatar of dynamicOne
dynamicOne

ASKER

This is on a raspberry pi. It should return a value of 1 or 0.
So it is returning 1.
Avatar of dynamicOne
dynamicOne

ASKER

Yes, it will return 1 when the door is open. Am I writing the condition statement wrong?
ASKER CERTIFIED SOLUTION
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of dynamicOne
dynamicOne

ASKER

When type gpio read 1 at the console it outputs 0 if the door is closed and 1 if it is opened
Avatar of dynamicOne
dynamicOne

ASKER

I found a script and was able to modify it to get what I want, is it okay with you if I post it here with my question or should I close this out and open another question?
Technically, you should start a new question if it is a different issue. That's a better way to get more eyes on the issue anyway.
I see the actual problem now that I saw the other script. You should have been checking if it was == to "0" instead of 0. But I think you see that now too.
Programming Languages-Other
Programming Languages-Other

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. Thousands of different programming languages have been created, mainly in the computer field, and many more still are being created every year. The description of a programming language is usually split into the two components of syntax (form) and semantics (meaning). Some languages are defined by a specification document (for example, the C programming language is specified by an ISO Standard), while other languages (such as Perl) have a dominant implementation that is treated as a reference. Some languages have both, with the basic language defined by a standard and extensions taken from the dominant implementation being common.

20K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo