getch() in Python displaying wrong output - python

I tried running this code in python
from msvcrt import getch
while True:
char = getch()
print char
but this is displaying the character 'ΓΏ' infinitely.
Can anyone help me with this
Thanks in advance

I don't blame you for wanting to use IDLE for debugging but PDB, while not as convenient, can do anything that IDLE can do and it doesn't have this problem (at least in Windows XP-W10). Under PDB both kbhit and getche work correctly. Whether debugging or not, in W10 your script's first use of getch returns junk before the user presses a key but subsequent use is clean. This isn't necessary in earlier versions of Windows but it is always a good idea to precede your real use with if kbhit() : getch() to ensure a clean buffer before asking for user input.

It just works fine when I run the same program via command line.
I believe you are trying in an IDE? If you are using pycharm, the same query has been posted to them and it is unanswered for like a year.
Link : Using getch() in pycharm
Pls try the same code in command prompt and let us know.
Updated answer :
getch() requires a working "console" window, and when you run Idle you don't get one as its set to use pythonw.exe. Thus, it's technically not able to process any keystrokes and returns immediately. You could try to verify that by starting Idle from the command line with the standard python. (But you may have to be in that console window to trigger the getch() to return)
I personally verified your program by launching IDLE from command line. Well it does not return that character indefinitely atleast. It doesnt return anything for that matter.
Just out of curiosity , do you really need to use IDLE for this program? Wouldn't the regular command line suffice? If yes, am sorry I couldnt be of much help for this question.
To launch idle from command prompt : python C:\Python27\Lib\idlelib\idle.py
Source for the answer : IDLE does not return getch() characters

Related

Circuitpython "interactive" option

I'm trying to debug a circuitpython program (that's running on a microcontroller) and I would like to know if there's a simple way to get the program to drop into the REPL upon a crash/termination while preserving the variables and functions defined in the script.
If this was a regular python program I would simply run it with the "interactive" option of the interpreter set : python -i my_code.py and then have access the variables and function defined in my code for easy debugging.
Instead what I get right now is: after a crash I get prompted to press a key to enter the REPL but the memory is cleared from any trace of my previously running code.
A somewhat cumbersome way to achive an equivalent behaviour, that only works if the code terminates and doesn't crash, is to proceed as follows :
Upload the code
Code will start running automatically
Interupt the code with a keyboard interupt
Press a key to get to the REPL
Import all from the code from the REPL by typing in:
from code import *
Wait for the code to terminate
Finally debug
Rince and repeat for each bug you find...

How do I make a python shell start running

I wrote a program in my editor and clicked run(F5). Afterwards, in the python shell, I keyed in the desired inputs. What should i do next to make it start running? Attached picture is python editor and python shell.
Thanks!
Use CTRL-D to stop entering new sys.stdin.read() input. Also don't use this.
https://teamtreehouse.com/community/whats-the-difference-between-sysstdinread-and-input

Python shell issue when using Data Nitro

I am using DataNitro to write Python Script in Excel. Its very useful indeed. However, when I open the Idle editor in excel, the accompanying Python Shell is not interactive, in that it does not return print statements, show errors, nothing. It just restarts every time I run the programme. This makes it incredibly hard to debug as I can't use print statements to trace the errors.
Does anyone know if this is a bug with DataNitro, or is it supposed to be that way, or whats going on? are there any solutions?
Thanks so much
Our IDLE editor is just an editor - it doesn't work as a shell.
The best way to debug programs is to raise an exception. This will freeze the shell that opens when a script is run, and you'll be able to inspect the variables and see any print statements that were generated during execution.
For example, if you run:
print Cell("A1").value
x = Cell("B1").value
raise
You'll see the value of A1 printed to the shell, and you can enter "x" at the prompt to see the value of B1.
You can also import a script you're working on into the regular Python shell (the one that opens when you press "shell"). This will execute the code in that script.
We'll be adding a guide to debugging code to the site soon, as well as some features that make it easier.
Source: I'm one of the founders of DataNitro.
Not as knowledgeable as Ben, but have been using DataNitro quite a bit and here are some tips:
The shell automatically closes once the script has run. If you want to inspect some prints or even interact with the shell I normally place following at end of my script.
raw_input("Press Enter to Exit shell")
Not very elegant, but I have even created a small loop that displays text options in the console. Can then interact with your program and sheet from there. Clever and more elegant way would be to have your script poll an excel cell and then take action form there.
Something else that you might find nice is that it also also you to run Ipython instead of the default python shell. Cannot imagine using python without Ipython... so you get benefits of tab completion Ipython debugging etc. To activate that just click the "Use Ipython" Checkbox in DataNitro Settings (don't know if this is version dependent).

python win32api.LoadKeyboardLayout(), doesnt change language in IDLE shell?

Can someone help me out please...I'm trying to start my first programming project. It will be implemented in python.
I need to have a textbox (which i am using wxpython for). If the user enters any text into this text box, then I want it to appear as arabic. I wanted to this by automagically changing the users Keyboard to an arabic layout when the cursor lands in the given text box.
So i found this pywin32 module, which has a function LoadKeyboardLayout()
So i am trying to test this in IDLE, to see if I can make it accept arabic text into IDLE, to see if it works. So I enter, into IDLE:
win32api.LoadKeyboardLayout('00000401',1)
This then returns, 67175425, the decimal equivalent of hex:'4010401' whcih I believe is the locale ID for Arabic. SO I think wow! I've done it, but when I try typing after this, in the IDLE window, it continues to type normal english characters.
Can someone please explain my errors and guide me towards a good solution.
UPDATE
Okay, I've been trying to solve this problem ever since posting the damn question.
No luck.
Then, I thought, "ok, screw it, instead of testing it quicly in IDLE, I will just try it out, in situ, in my source code for the project."
WTF - it worked first time, giving exact behaviour that I wanted.
Then I tried it in a different IDE, in the interpreted window, and again, IT WORKED straight away!
So clearly my issue is with IDLE, in its interpreting mode.
Can anyone explain why it doesn't work in the IDLE shell???
Keyboard layout setting in Windows is per-process (and inherited from the parent process)
IDLE runs your Python script in a background process separate from its GUI
So you have successfully changed the keyboard layout of the background Python process that is running your script, but not of IDLE's GUI.

indent with 'tab' button in python commandline

hi everyone :)
my problem is, up until now, i have exclusively used tabs to indent python, as i find it easier than spaces, but for no reason i know, python interactive prompt, the basic python.exe one, suddenly refuses to accept the tab button, all it does is flash the cursor. all i can think of is that my computer in suddenly treading the window like any other, using tab to cycle input things, in this case the single one. also, before now, i could use the up button to reach previously typed code, the if i submit that line with no changes, use the down button to access the line that came after it, but now up works, but as if i had changed the line, eg moves me back to the "bottom" of the list of inputs, so down doesn't work.... my question simply is: how do i get my good old tab and down button to work like i want them to again? :(
thanks xxx
If you are using Windows with the standard cmd.exe console (and it would have been helpful for you to have stated this up front) then you can use the TAB and arrow keys exactly as you desire.
I recently observed this behavior too, on Windows, using cmd.exe. It also happens with Console2 - an alternate shell I sometimes use.
Though I do always use spaces in normal code in an editor, I had been accustomed to using the Tab key to indent in short multi-line inputs in the interactive python.exe interpreter. Recently that stopped working - pressing the Tab key flashes the cursor and doesn't indent. Using spaces does work fine here, though it's not as convenient past a couple of indentations.
I suspect (but am not certain) that the cause was installation of pyreadline or rlcompleter - I had been messing with trying to get tab completion in an interpreter in an embedded application on Windows. Of course in your case another installation could have included those packages.

Categories

Resources