将代码编写为:“t = int(input())for _ in range(t)”在某些测试用例中出错,但是“for _ in range(int(input())”它运行正常!差异?

时间:2018-04-27 00:19:08

标签: python-3.x

例外名称:EOFError 异常消息:读取行时的EOF 代码在某些测试用例中失败,因为此错误显示在第1行,测试用例在总是不同的地方失败:

t=int(input())
for _ in range(t):
    n=int(input())
    x=n%12
    if x==1:
        print(n+11,"WS")
    elif x==2:
        print(n+9,"MS")
    elif x==3:
        print(n+7,"AS")
    elif x==4:
        print(n+5,"AS")
    elif x==5:
        print(n+3,"MS")
    elif x==6:
        print(n+1,"WS")
    elif x==7:
        print(n-1,"WS")
    elif x==8:
        print(n-3,"MS")
    elif x==9:
        print(n-5,"AS")
    elif x==10:
        print(n-7,"AS")
    elif x==11:
        print(n-9,"MS")
    elif x==0:
        print(n-11,"WS")

这是通过所有测试用例的代码:

for _ in range(int(input())):
    n=int(input())
    x=n%12
    if x==1:
        print(n+11,"WS")
    elif x==2:
        print(n+9,"MS")
    elif x==3:
        print(n+7,"AS")
    elif x==4:
        print(n+5,"AS")
    elif x==5:
        print(n+3,"MS")
    elif x==6:
        print(n+1,"WS")
    elif x==7:
        print(n-1,"WS")
    elif x==8:
        print(n-3,"MS")
    elif x==9:
        print(n-5,"AS")
    elif x==10:
        print(n-7,"AS")
    elif x==11:
        print(n-9,"MS")
    elif x==0:
        print(n-11,"WS")

当您在循环中获取输入并将输入置于外部输入时,是否存在差异?

1 个答案:

答案 0 :(得分:1)

您的两个代码段都在提高EOFError,但这是因为我在键盘上按下了 Ctrl + D ,并且input()被设计为在这种情况下提高EOFError。但是,如果我在每行上键入数字而没有按 Ctrl + D ,那么您的两个代码段都可以正常工作。

要回答您的问题,在循环内还是循环外调用input()都没有区别。由于您要处理的是终端的标准输入,因此,EOFError来自于按 Ctrl + D