Hello i worte this simple program for encripting stings. It requires from user to enter a simple string after doing that user shoud hit EOF shortcut (CTRL+D or CTRL+Z) but nothing than entering ^D or ^Z happens. Everything is fine when I do this on Ubuntu (using CTRL+D), but on windows this problem occurs. (Windows 10, python 2.7) Is there any other way to enter EOF in userinput in console. Program:
import string
table = string.maketrans("abcdefghijklmnopqrstuvwxyz", "nopqrstuvwxyzabcdefghijklm")
import fileinput
for line in fileinput.input():
line = line.rstrip()
print string.translate(line, table)
You can use sys.stdin.readline()
that provides automatic EOL Once you hit CTRL D
, the program will quit gracefully.
Modified Code
import string
import atexit
import sys
#import fileinput
table = string.maketrans("abcdefghijklmnopqrstuvwxyz", "nopqrstuvwxyzabcdefghijklm")
while True:
line = sys.stdin.readline() #readline returns EOF by default after read
if line: #If data is read
line = line.rstrip() #read line and remove whitespace
print string.translate(line, table) #print translated string
else:
sys.exit(0) #exit on 'CTRL D'
Program Output
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
wefjdjbgngfpbgf
jrswqwotatscots
jgnfrejgrtnhkeritrevndsksdlv
wtaserwtegauxrevgeriaqfxfqyi
>>>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.