DominusPi
Posts: 39
Joined: Mon Nov 18, 2013 5:31 pm

Problem programm freezes

Thu Jan 23, 2014 11:33 pm

PROBLEM SOLVED ! (see end for explanation)
Hey guys !
I'm having some issues with a little programm I wrote, probably because I'm new to python.
I'll put the code first so it's easier to explain :

Code: Select all

#! /usr/bin/env python


import sixaxis
import pprint
import time

sixaxis.init("/dev/input/js0")
pp = pprint.PrettyPrinter(indent=4)
stop = True
while(stop = True):
    state = sixaxis.get_state()
    if(state['triangle'] == True):
        stop=False
    pp.pprint(state)
    time.sleep(0.1)
What happens is eveything goes fine, and is updated every 0.1 sec until I press triangle.

At that moment the program freezes, I can see from the last print that triangle is True, and so it never "finishes" and I'm obbliged to do ctrl+z (ctrl+c doesn't work, any ideas why ?)

Thanks !

EDIT : If anyone gets the same problem, it's due to a line missing : sixaxis.shutdown()
Last edited by DominusPi on Thu Jan 23, 2014 11:57 pm, edited 1 time in total.
Cheers !

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Problem programm freezes

Thu Jan 23, 2014 11:54 pm

Once you hit "triangle" you set "stop" to False. The loop does not stop at that point, and so your print and sleep lines will run one more time. At that point, your loop condition is no longer true and so will not repeat.

Can you also try changing your loop to "while stop". I have two concerns with your current wording:
1) usually you test whether a variable has a value with two = signs (I'd actually expect a syntax error with the one equal sign...); however
2) if testing for a boolean then you don't need to say "== True".

Also, it's a bit grammatically confusing to say "while stop" - maybe switch it around so stop is False and is then set to True when you press triangle, you can then do your loop with "while not stop". (That's just style and ease of reading (and me being a bit sad...))
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

DominusPi
Posts: 39
Joined: Mon Nov 18, 2013 5:31 pm

Re: Problem programm freezes

Fri Jan 24, 2014 12:00 am

elParaguayo wrote:Once you hit "triangle" you set "stop" to False. The loop does not stop at that point, and so your print and sleep lines will run one more time. At that point, your loop condition is no longer true and so will not repeat.

Can you also try changing your loop to "while stop". I have two concerns with your current wording:
1) usually you test whether a variable has a value with two = signs (I'd actually expect a syntax error with the one equal sign...); however
2) if testing for a boolean then you don't need to say "== True".

Also, it's a bit grammatically confusing to say "while stop" - maybe switch it around so stop is False and is then set to True when you press triangle, you can then do your loop with "while not stop". (That's just style and ease of reading (and me being a bit sad...))
Thanks for answering anyway !
Just to answer you, as a C programmer I know the little tricks such as no ==True when it's a bool, I just put it cause I wasn't sure it applied in python and guessed it was maybe given to that line.
Have a nice day !
Cheers !

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Problem programm freezes

Fri Jan 24, 2014 12:09 am

Ha! Then you can just ignore everything I said - glad to see you got it fixed.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”