Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4146

Python • Re: How do I fix this Python code?

$
0
0
I added the

Code:

try:
and it didn't say "syntax error" but when I ran the code it recorded fine but when I stopped it, it still was recording. It was supposed to stop recording when I stopped the code but it didn't. Any ideas how to fix?
How are you stopping the code ?

Assuming the code as you have it is as below, and things work as hoped -

Code:

try:    send_command("Record1stChoice")except KeyboardInterrupt:    send_command("Stop")    sys.exit()
It will send the "Record1stChoice", won't error, will continue at the line after 'sys.exit()', won't ever send a "Stop" command.

If that is your code, there's nothing after it, it should automatically exit to the command line after sending the "Record1stChoice", no need to explicitly stop the code.

It seems to be behaving as expected. You would need a 'while True: pass' after sending "Record1stChoice" so you then have to use Ctrl-C to have the exception handle that and send the "Stop" command. Something like this though not correct use of error trapping IMO -

Code:

try:    send_command("Record1stChoice")    while True:        passexcept KeyboardInterrupt:    send_command("Stop")    sys.exit()
I would suggest -

Code:

send_command("Record1stChoice")try:    while True:        passexcept KeyboardInterrupt:    passsend_command("Stop")

Statistics: Posted by hippy — Tue Feb 27, 2024 3:36 pm



Viewing all articles
Browse latest Browse all 4146

Trending Articles