IndexError: List index out of range, only when performing operations 2 list items - Python -
i have following code:
infile = open("sitin.txt", "r") r = int(infile.readline().split()[0]) s = int(infile.readline().split()[1]) totalseats = r * s print(totalseats)
my input file, "sitin.txt" text file nothing 10 10
. printing either r or s returns correct value, , printing either r or s multiplied 10 returns correct value, code, attempting multiply r , s returns "indexerror: list index out of range". happening here?
you read input file many times; r
first line; s
second line (and on)... fix:
infile = open("sitin.txt", "r") split = infile.readline().split() # read once only! r = int(split[0]) s = int(split[1]) totalseats = r * s print(totalseats)
you may need check split
has correct form input.
Comments
Post a Comment