I want to implement the input taking process of $cat in Unix. Inside an infinite loop, whenever I'll press any key, the corresponding letter will be printed on the screen. If I press ctrl+d, loop will get terminated.
This TurboC code does exactly what i want:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
while(1)
{
ch = getch();
if(ch == 4)
break;
else
printf("%c", ch);
}
}
But whenever I'm switcihing into Python3, it's creating problem.
from msvcrt import getch
while True:
ch = getch()
if ord(ch) == 4: break
else: print(ch.decode(), end="")
When the program is in infinite loop, it doesn't print anything but I'm pressing keys. Finally when I press ctrl+d, then all previously inputted characters are getting printed together.
How can I implement that in Python?