我做了一个程序:
import collections
x = input("Enter in text: ")
x_counter = collections.Counter()
x_counter.update(x)
print("Your sequence contains:\n")
for i in '`1234567890-=qwertyuiop[]\asdfghjkl;zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP\
{}|ASDFGHJKL:"ZXCVBNM<>?':
print(i, x_counter[i])
打印出文字中使用字母的次数。 当用户输入较小的文本时,例如段落......程序运行正常。当用户输入一个很长的文本时,说5个段落...程序退出并运行所有输入作为bash命令...为什么这个???
答案 0 :(得分:5)
这是因为input
只从用户获得一行,如下例所示:
pax> cat qq.py
x = raw_input ("blah: ") # using raw_input for Python 2
print x
pax> python qq.py
blah: hello<ENTER>
hello
pax> there<ENTER>
bash: there: command not found
pax>
一种可能性是从文件中读取信息而不是使用input
,但您也可以执行以下操作:
def getline():
try:
x = raw_input ("Enter text (or eof): ")
except EOFError:
return ""
return x + "\n"
text = ""
line = getline()
while line != "":
text = text + line;
line = getline()
print "\n===\n" + text
将继续读取用户的输入,直到他们用EOF结束输入(Linux下的CTRL-D):
pax> python qq.py
Enter text (or eof): Hello there,
Enter text (or eof):
Enter text (or eof): my name is Pax.
Enter text (or eof): <CTRL-D>
===
Hello there,
my name is Pax.