Python Forum
Help to understand my mistake
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help to understand my mistake
#1
Hello to all the community, if I allow myself to write to you, it is to obtain help concerning the errors, which I obtain by launching my script and whose origin I can not understand.
To explain the purpose of this script, it will help rescue services (in search of aircraft tags) by helping them to more easily share their geographic coordinates at the checkpoint.
Using a GPS module that will have the task of retrieving the information, the various information will be displayed via an LCD screen.

  File "GPS.py", line 256
    while (index = 1):
        ^
SyntaxError: invalid syntax
I initially thought that it was a problem of indentation, but after several tests, I always get the same error; so I came to the conclusion that it was better for me to ask for help to solve this problem.

Regarding the code itself, I share directly the link to my GitHub (here).
I hope someone could help me, to finish this project so that I can share it to the services that will need it most in their research ! Cool
Reply
#2
please show code in context.
immediately, no parenthesis around index, and use == , not =
Reply
#3
(Jul-04-2019, 04:02 PM)Larz60+ Wrote: please show code in context.

I'm sorry but I do not understand this question. In the context of use? Well, I run the script and I get this error. Huh Huh
Reply
#4
Hi,

@TeeMan: although the line you show is not good / wrong Python, as Larz60+ explained, the actual error which causes the syntax error is not in this line. It is in one of the lines before. Thus the request to show more code. Otherwise, nobody can help you.

Regards, noisefloor
Reply
#5
(Jul-04-2019, 05:31 PM)noisefloor Wrote: Thus the request to show more code. Otherwise, nobody can help you.
He did post a link to all code on Github in his post.

@TeeMan the code has several errors and do not look good,SyntaxError is always simple errors.
Do not 1-space as indentation,in Python is always 4-space.
To clean most up,i can of course not run this code for testing.
def main():
    index = 0
    gpsp = GpsPoller() # create the thread
    try:
        gpsp.start() # start it up 
        while index == 1:
            lcd.lcd_clear()
            printMessage_1()
            time.sleep(2)   
            lcd.lcd_clear()
            printMessage_2()
            time.sleep(2)   
            lcd.lcd_clear()
            printMessage_3()
            time.sleep(7)   
            lcd.lcd_clear()
            printMessage_4()
            time.sleep(2)   
            lcd.lcd_clear()
            printMessage_5()
            time.sleep(3)   
            lcd.lcd_clear()
            printMessage_6()
            time.sleep(3)   
            lcd.lcd_clear()
            printMessage_7()
            time.sleep(3)   
            lcd.lcd_clear()
            printMessage_8()
            time.sleep(5)   
            lcd.lcd_clear()
            printMessage_9()
            time.sleep(3)   
            lcd.lcd_clear()
            printMessage_10()
            time.sleep(5)
            index += 1
        while True:
            lcd.lcd_clear()
            printMessageTime()
            time.sleep(7)
            lcd.lcd_clear()
            printMessageAltitude()
            time.sleep(7)
            lcd.lcd_clear()
            printMessageQTH()
            time.sleep(7)
    except Exception as error:
        print(error)         

if __name__ == '__main__':
    try:
        main()
    except (KeyboardInterrupt, SystemExit):
        gpsp.running = False
        gpsp.join()
You had a try without a except.
So added this to avoid SyntaxError.
except Exception as error:
    print(error)   
Reply
#6
If you're just going to print the error object, I recommend forgoing your exception handling and just letting the program crash. As-is, you'd have to modify the code to investigate the issue, but if you let the error propagate then you'd have a lot of useful info when trying to debug.
Reply
#7
(Jul-04-2019, 06:09 PM)snippsat Wrote: Do not 1-space as indentation,in Python is always 4-space.
I admit to having only small bases in Python, certainly, I know a few things, but you have just taught me something ! Wink But why necessarily 4 spaces and not one ? Confused

In any case, it seems to work so thank you very much, I just get the following error, but I will look at my side, I probably have trouble copying something somewhere; out of curiosity, I give you my model concerning this part (here), without doubt I have it badly adapted :

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "GPS.py", line 38, in run
    while gpsp.running:
NameError: global name 'gpsp' is not defined
Reply
#8
Hi,

Quote:But why necessarily 4 spaces and not one ?
In Python, a couple of things are by convention, including things like indenting with four spaces (and always and only four spaces). Good Python programmers are picky about that - and that's correct. The general style guide for Python is the PEP 8, the holy grail of Python programming. Just read through it.

Line 31 is super wrong, using global like this is in a class is super wrong. Seems like there is a heavy lack on your side on understanding Python's classes and what attributes of classes are for. I would recommend to read through that in the official Python documentation.

Generally speaking, using global is wrong in 99% of the cases, it's just to a poor design of the program. Avoid global, as it makes it very hard to track the state of your program.

Other things:
Function names are written lowercase_with_underscore in Python, not camelCase or so.

When you start the number things like printMessage_1, printMessage_2, it's a code smell and you most likely want to use a different structure. In your case, all the printMessages have highly redundant code. Use _1_ function only and pass the message to be print e.g. from a dict or a list storing the actual text.

Line 256 is not a valid Python expression, see above. Except this, the while-loop will never run ...

Line 38 should give you a NameError, as gpsp is unknown in the scope of the method.

What's the purpose of line 313? If you intend to stop the thread -> it will not.

Regards, noisefloor

Some of your other functions are too long, they do too much things at a time. I would recommend to break them down in smaller chunks of code.
Reply
#9
(Jul-05-2019, 11:58 AM)noisefloor Wrote: The general style guide for Python is the PEP 8, the holy grail of Python programming. Just read through it.

To be quite honest, for the most precise functions (the GPS thread, and its functions); I had to retrieve pieces of code that dragged on the internet I managed to assemble them to make something that seems functional (it's a little my way of learning). It is true that I do not know, effectively use the Python language, but I am ready to learn from my mistakes! I'm going to look at what exactly the PEP 8 can teach me !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Solved] Please, help me find a simple mistake AlekseyPython 2 1,701 Jun-17-2021, 12:20 PM
Last Post: AlekseyPython
  [split] Could you please clarify where i did mistake also how run without admin right Abubakkar 1 1,753 Jun-14-2021, 09:32 AM
Last Post: Larz60+
  Please help to me to find my mistake in code leonardin 2 1,801 Nov-29-2020, 04:17 PM
Last Post: Larz60+
  minor mistake in code for factorial spalisetty06 2 1,854 Aug-22-2020, 05:00 PM
Last Post: spalisetty06
  Simple mistake about for Nomatter 4 2,183 Jul-16-2020, 02:24 PM
Last Post: Nomatter
  Install Mistake jlerette5 1 1,858 Feb-18-2020, 12:19 AM
Last Post: jefsummers
  What was my mistake in this Python code (easy)? voltman 4 3,382 Nov-19-2019, 09:58 PM
Last Post: snippsat
  countdown script not working..plz help what is mistake randyjack 1 2,079 Oct-28-2019, 06:57 AM
Last Post: perfringo
  Beginner mistake. bbweeg 1 2,003 Aug-17-2019, 07:27 AM
Last Post: perfringo
  Need to find a mistake in my code boris602 3 3,092 Jan-11-2018, 01:49 PM
Last Post: boris602

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020