我如何通过在python 3中结束文件来终止输入 [英] How do I terminate an input by ending the file in python 3
本文介绍了我如何通过在python 3中结束文件来终止输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理的代码是一个简单的减法工作,但是当我需要结束输入时出现了我的问题。
The code I am working on is a simple subtraction work but my problem arises when I need to end the inputs.
该问题告诉我输入已终止到文件末尾,我不知道当问题有任意数量的输入时这意味着什么。
The problem tells me that the input is terminated by end of file and I have no idea what that means when the question has any number of inputs.
while(True):
data=[]
data= list(map(int,sys.stdin.readline().split()))
if (data[1]>data[0]):
total=data[1]-data[0]
else:
total=data[0]-data[1]
print(total)
如何更改此代码以使其在无需用户输入任何内容的情况下结束
How do I change this code to allow it to end without the user inputting anything
推荐答案
根据注释中的建议,在 sys.stdin
上使用循环。我还添加了其他一些改进:
As suggested in comments, use loop over sys.stdin
. I've also added some other improvements:
for line in sys.stdin:
data = [int(x) for x in line.split()]
total = abs(data[1] - data[0])
print(total)
这篇关于我如何通过在python 3中结束文件来终止输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文