indentation error

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 605 Views - Last Post: 18 August 2014 - 02:42 PM Rate Topic: -----

#16 sachin.sunny96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 14

Re: indentation error

Posted 13 August 2014 - 08:45 AM

both statements should lead to
speak("move either left or front")

I mean both statements should work independently
Was This Post Helpful? 0
  • +
  • -

#17 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3614
  • View blog
  • Posts: 12,437
  • Joined: 12-December 12

Re: indentation error

Posted 13 August 2014 - 08:47 AM

If you take my advice that lines following a colon: need to be indented then it is the first version that is correct.
Was This Post Helpful? 1
  • +
  • -

#18 sachin.sunny96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 14

Re: indentation error

Posted 13 August 2014 - 08:51 AM

thank you. I will run the code on raspberry pi and will post the result.

Thank you again for your help.
Was This Post Helpful? 0
  • +
  • -

#19 Marbelous  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 40
  • Joined: 12-November 12

Re: indentation error

Posted 13 August 2014 - 09:09 AM

Cross-posted at python-forum: http://python-forum....&p=18771#p18771

The part about the GPIO pin logic is ambiguous since we don't know what's supposed to physically happen. If you don't know Python, where did you get this RPi code??? You need to go back there and get the indentation. Again, indentation is CRITICAL in Python. It can and will change the logic of your program.

Of course in this simple case you can just try out the possibilities and see which one functions correctly.

Assuming this is some sort of robot sensor the indentation you want is probably like this:

while 1:
    left = GPIO.input(11)
    front = GPIO.input(12)
    right = GPIO.input(13)
    if left == True or front == True or right == True:  # Check to see if any direction is available
        if left == False and front == False and right == True: # Further check to see if right is blocked but left and front are possible
            speak("move either left ot front")
        else:
            speak("you can move in any direction")

This post has been edited by Marbelous: 13 August 2014 - 09:13 AM

Was This Post Helpful? 1
  • +
  • -

#20 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3614
  • View blog
  • Posts: 12,437
  • Joined: 12-December 12

Re: indentation error

Posted 13 August 2014 - 09:10 AM

I notice that the OP failed to add code-tags in the other forum as well :whistling: oh well.
Was This Post Helpful? 0
  • +
  • -

#21 Marbelous  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 40
  • Joined: 12-November 12

Re: indentation error

Posted 13 August 2014 - 09:19 AM

View Postandrewsw, on 13 August 2014 - 11:10 AM, said:

I notice that the OP failed to add code-tags in the other forum as well :whistling:/>/> oh well.


Well, I guess we can cut him some slack since he's an "Electronics" guy. I am too so I know how sometimes it takes us a while to function in the virtual world. :1eye:/>
Was This Post Helpful? 0
  • +
  • -

#22 sachin.sunny96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 14

Re: indentation error

Posted 13 August 2014 - 10:08 AM

line 3,

AttributeError: 'module' object has no attribute 'setwarnings'

what does it mean?
Was This Post Helpful? 0
  • +
  • -

#23 fryishone  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 53
  • Joined: 09-May 14

Re: indentation error

Posted 13 August 2014 - 10:09 AM

It means... the object named 'module' does not have an attribute named 'setwarnings'

This post has been edited by fryishone: 13 August 2014 - 10:10 AM

Was This Post Helpful? 0
  • +
  • -

#24 Marbelous  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 40
  • Joined: 12-November 12

Re: indentation error

Posted 13 August 2014 - 02:24 PM

View Postfryishone, on 13 August 2014 - 12:09 PM, said:

It means... the object named 'module' does not have an attribute named 'setwarnings'

True, but the RPi.GPIO module does have an attribute named setwarnings(bool). I suspect you haven't imported it properly. The GPIO module REQUIRES super-user privileges to import. You must run your python script as superuser (or with the 'sudo' prefix.

See here for more info: http://sourceforge.n...iki/BasicUsage/

Change your
import RPi.GPIO as GPIO



to
try:
    import RPi.GPIO as GPIO
except RuntimeError:
    print("Error importing RPi.GPIO!  This is probably because you need superuser privileges.  You can achieve this by using 'sudo' to run your script")


so that you always remind yourself to be superuser when running RPi GPIO scripts.

This post has been edited by Marbelous: 13 August 2014 - 02:25 PM

Was This Post Helpful? 0
  • +
  • -

#25 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3614
  • View blog
  • Posts: 12,437
  • Joined: 12-December 12

Re: indentation error

Posted 13 August 2014 - 02:28 PM

Just to note that the OP is (or was) attempting to run as super-user with:
$ sudo python filename.py

(although his filename now has a space in it).

This post has been edited by andrewsw: 13 August 2014 - 02:29 PM

Was This Post Helpful? 0
  • +
  • -

#26 Marbelous  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 40
  • Joined: 12-November 12

Re: indentation error

Posted 13 August 2014 - 09:01 PM

View Postandrewsw, on 13 August 2014 - 04:28 PM, said:

Just to note that the OP is (or was) attempting to run as super-user with:
$ sudo python filename.py

(although his filename now has a space in it).

Thanks, I actually just noticed that after checking the python-forum thread. Well, that's strange. I'm posting from a Raspberry Pi and I just tried the same code and it worked fine as usual.
Was This Post Helpful? 0
  • +
  • -

#27 sachin.sunny96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 14

Re: indentation error

Posted 17 August 2014 - 08:25 AM

Is it working?
when i type :
sudo python myfile.py

It is showing error as:
Tracebck (most recent call last):
File "myfile.py", line 3, in <module>
GPIO.setwarnings(False)
AttributeError: 'module' object has no attribute 'setwarnings'

My raspberry pi is model B.
I had downloaded "RPi.GPIO-0.1.0.tar" and installed it.

can you please tell me a solution?

This post has been edited by andrewsw: 17 August 2014 - 08:28 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#28 Marbelous  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 40
  • Joined: 12-November 12

Re: indentation error

Posted 18 August 2014 - 02:42 PM

I have a couple Raspberry Pi units and have no problems with the GPIO. If you're not getting any import errors I have no idea why you're getting an attribute error with the GPIO.setwarnings() function.

Your problem might be that you downloaded and installed a TAR file instead of just going through the Raspian OS. The GPIO code was probably already on your Pi and if not it IS in the repository where you know what you're getting. Try this on the command line and get a fresh, new version.

sudo apt-get update
sudo apt-get -y install python-rpi.gpio

Then reboot and try your code again.

I also suggest you simplify your code down and work on getting an LED to blink and reading a push-button switch. Then, once that's working you can add back in the rest of you functionality.

WARNING: There is a 5V line on the GPIO header but the IO is 3.3V and you will destroy it if you connect 5V to any of the pins directly. Google "Raspberry Pi GPIO tutorial" and read up on how to use the port. I also recommend the MagPi magazine for lots of fun tutorials and projects: http://www.themagpi.com/

Go here and do a text search for "GPIO" to read some good tuts: http://magpiindex.st...co.uk/index.php
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2