Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the right method to read from input file and perform subtraction?

I am a beginner computational thinking programmer and am doing some basic problems for Australian Informatics Olympiad practice problems.

Here’s the link to the question

This is my code so far:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()

Here’s the error I get:

Traceback (most recent call last):
  line 25, in <module>
    if t > seats_available:
TypeError: '>' not supported between instances of 'map' and 'int'

Any help would be much appreciated!

>Solution :

this will result as expected

#example line: "2 3" => r = 2, s = 3
r, s = map(int, inputfile.readline().split()) 

the problem is in this line:

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])

or

t,dummy = map(int, inputfile.readline().split())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading