Reputation: 15
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:
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!
Upvotes: 0
Views: 53
Reputation: 3462
You should use with open
structure to open files so you don't have to worry about closing them. Also, initializing the values with 0 is something that you don't have to do so I dropped that too. These few things make your code easier to read and less error prone.
You are getting the error because on the line for t
there is only a single value, so it is not automatically unpacked like r
and 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")
Upvotes: 1
Reputation: 365
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())
Upvotes: 1