0
[Solved] Python script works differently on IDLE and command prompt
I made a little code today with a standard module only comes with MS Windows, winsound. The code prints a character and the make a beep. It works perfectly on IDLE. However when I run it in command prompt / powershell, it prints all the characters at once after all the beep sound finished. What's give? https://code.sololearn.com/cMsH6aKZt57E/?ref=app
7 odpowiedzi
+ 1
Wong Hei Ming
great link. The -u option and the PYTHONUNBUFFERED env variables are good to know ..👍
Also, using functools partial is a great idea. I feel it's like a quick and simple decorator when used this way.
Cross-platform beep is harder. From what I found out, it seems that audio playback is the only reliable way to do it, as beeps are implemented too differently in various os. Another option seems to use Pygame, but that is just overkill...
+ 3
Wong Hei Ming
i found that it works like you wanted if print is used normally (terminated with '\n'). But if you use end='', all the beeps finish then the whole line is printed.
so you need to do a flush operation.
use
print(code, end='', flush=True)
https://code.sololearn.com/cR10n13WmlyV/?ref=app
+ 2
Bob_Li
Base on your feedback I found this article.
https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-flush-print-output/
+ 1
print('\a')
is supposed to trigger a system beep sound. But it may have a different effect on each platform, and not possible to set the length.
For a true cross platform solution... sadly, none exist.
https://python-list.python.narkive.com/p9V26mRv/howto-do-a-robust-simple-cross-platform-beep
https://www.reddit.com/r/learnpython/comments/k04eke/making_beeps_and_sounds_with_python_2020_still/
+ 1
Bob_Li
The nested number is fixed, probably muscle memory after keying 26 letters’ code.
It does separate part with 1 unit, letter with 3 units and word with 7 units.
Uncomment line 108 and 115 will reveal the action.
To observe part separation, change the number from 500 to 1500 in line 71.
I also added a fail-safe in case the user input a string with punctuation. It removes any punctuation from the message and reconstructs the end result.
0
Tibor Santa
The code doesn't use print('\a') to make a beep, it use winsound module and it is Windows exclusive.
In line 95 and 96 it is programmed to print a dot or a dash first, and then make the beep.
In IDLE it is . beep . beep _ beep
In prompt it sounds all the beep, then print the dot and dash.
The behaviou is different but on the same machine and same OS, only the run-time environment different.
0
Wong Hei Ming
your split does not keep the empty spaces between words
maybe use re.split?
also I spotted nested quotes in the MORSE dictionary '"1"':
also maybe add the space as a key
' ':' ' #7 units between words?
import re
def beeping(msg):
msg = re.split(r'(\s)', msg)
for word in msg :
for ch in word:
print(ch, end='', flush=True)
for sym in MORSE.get(ch):
print(sym, end='', flush=True)
sound(sym)
print(' ')
sound(' ')