这是一个创建于 1842 天前的主题,其中的信息可能已经有所发展或是发生改变。
如题,以前做多行输入的时候一直用的 while true 循环,通过 try 和 except 的报错机制控制连续的多行输入,也没什么问题,但是昨天用编译器的时候发现,不能跳出输入循环了……用 sys.stdin 的输入也不行,很纠结不知道是怎么回事,贴一个我的测试代码:
import collections
dict_sum = collections.OrderedDict()
while True:
try:
key = input().split('\\')[-1]
if key in dict_sum:
dict_sum[key] += 1
else:
dict_sum[key] = 1
except :
break
list1 = sorted(dict_sum.items(),key = lambda x:x[1],reverse = True)
print(list1)
n = 8
for item in list1:
if n>0:
print(item[0].split()[0][-16:],item[0].split()[1],item[1])
n += -1
执行输入以后疯狂摁回车也不能跳出 while 循环,现在真的有点烦躁,不知道怎么回事,这个代码我以前用一模一样的运行是没问题的……
5 条回复 • 2019-09-06 11:22:55 +08:00
|
|
1
arrow8899 2019-09-05 13:45:06 +08:00
if key == 'exit': break
|
|
|
2
Vegetable 2019-09-05 14:36:07 +08:00
你这哪有退出 while 的判断啊?根本没有退出条件,str.split("sth")返回的列表是永远不为空的,你那个 try 除了 ctrl+c 就不会捕捉到错误.
|
|
|
3
mushuiyu 2019-09-05 14:48:28 +08:00
@ Vegetable 就是在 try 下面就有一条 input ()的赋值语句啊,如果输入为空的话,就会报错然后跳到 except 那里去
|
|
|
4
676529483 2019-09-05 15:08:37 +08:00
是不是 py 版本的问题? py2 的 input 会报 EOF,py3 会返回空字符串,所以就退出不了了
|