从输入文件中读取并执行减法的正确方法是什么?
What is the right method to read from input file and perform subtraction?
我是初学计算思维的程序员,正在做澳大利亚信息学奥林匹克练习题的一些基础题
Here's the link to the question
到目前为止,这是我的代码:
inputfile = open("sitin.txt", "r")
outputfile = open("sitout.txt", "w")
r = 0
s = 0
t = 0
sit = 0
stand = 0
seats_available = 0
#Reading integers from input file
r, s = map(int, inputfile.readline().split())
t = map(int, inputfile.readline().split())
#Conducting the operations
seats_available = r * s
if t > seats_available:
sit = r*s
stand = t - seats_available
else:
sit = t
stand = 0
#Writing answer in the output file
outputfile.write(sit + "" + stand + "\n")
outputfile.close()
inputfile.close()
这是我得到的错误:
Traceback (most recent call last):
line 25, in <module>
if t > seats_available:
TypeError: '>' not supported between instances of 'map' and 'int'
如有任何帮助,我们将不胜感激!
结果如预期
#example line: "2 3" => r = 2, s = 3
r, s = map(int, inputfile.readline().split())
问题出在这一行:
t = map(int, inputfile.readline().split())
# results t = iterator, it doesn't unpack the result
# this should fix the problem:
t = int(inputfile.readline().split()[0])
或
t,dummy = map(int, inputfile.readline().split())
您应该 这样您就不必担心关闭它们。此外,用 0 初始化值是你不必做的事情,所以我也放弃了它。这几件事使您的代码更易于阅读且不易出错。
您收到错误是因为在 t
的行上只有一个值,因此它不会像 r
和 s
那样自动解包。
# Reading integers from input file
with open("sitin.txt", "r") as inputfile:
r, s = map(int, inputfile.readline().split())
t = int(inputfile.readline().strip())
#Conducting the operations
seats_available = r * s
if t > seats_available:
sit = r*s
stand = t - seats_available
else:
sit = t
stand = 0
#Writing answer in the output file
with open("sitout.txt", "w") as outputfile:
outputfile.write(f"{sit} {stand}\n")
我是初学计算思维的程序员,正在做澳大利亚信息学奥林匹克练习题的一些基础题
Here's the link to the question
到目前为止,这是我的代码:
inputfile = open("sitin.txt", "r")
outputfile = open("sitout.txt", "w")
r = 0
s = 0
t = 0
sit = 0
stand = 0
seats_available = 0
#Reading integers from input file
r, s = map(int, inputfile.readline().split())
t = map(int, inputfile.readline().split())
#Conducting the operations
seats_available = r * s
if t > seats_available:
sit = r*s
stand = t - seats_available
else:
sit = t
stand = 0
#Writing answer in the output file
outputfile.write(sit + "" + stand + "\n")
outputfile.close()
inputfile.close()
这是我得到的错误:
Traceback (most recent call last):
line 25, in <module>
if t > seats_available:
TypeError: '>' not supported between instances of 'map' and 'int'
如有任何帮助,我们将不胜感激!
结果如预期
#example line: "2 3" => r = 2, s = 3
r, s = map(int, inputfile.readline().split())
问题出在这一行:
t = map(int, inputfile.readline().split())
# results t = iterator, it doesn't unpack the result
# this should fix the problem:
t = int(inputfile.readline().split()[0])
或
t,dummy = map(int, inputfile.readline().split())
您应该
您收到错误是因为在 t
的行上只有一个值,因此它不会像 r
和 s
那样自动解包。
# Reading integers from input file
with open("sitin.txt", "r") as inputfile:
r, s = map(int, inputfile.readline().split())
t = int(inputfile.readline().strip())
#Conducting the operations
seats_available = r * s
if t > seats_available:
sit = r*s
stand = t - seats_available
else:
sit = t
stand = 0
#Writing answer in the output file
with open("sitout.txt", "w") as outputfile:
outputfile.write(f"{sit} {stand}\n")