Machina's blog

By Machina, history, 4 weeks ago, In English,

Why did this approach produce a Runtime Error?

skills = list()
queries = int(input())
for i in range(queries):
    students = int(input())
    if students == 1:
        print(1)
        continue
    for i in range(students):
        skill = int(input())
        skills.append(skill)
    for n in range(students — 1):
        for i in range(students):
            if abs(skills[n] — skills[i]) == 1:
                print(2)
                break
        else:
            print(1)
            continue
        break

Thanks in advance for any help

 
 
 
 
  • Vote: I like it
  • -4
  • Vote: I do not like it

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Machina (previous revision, new revision, compare).

»
4 weeks ago, # |
Rev. 2   Vote: I like it +2 Vote: I do not like it

input() reads a line, so does skill = int(input()). Numbers that represent skill values lie on the same string, therefore you're trying to convert something like 2 10 1 20 into an integer, which is not possible and gives runtime error.

Instead, you could use a list comprehension, such as skills = [int(x) for x in input().split()] or just like this: skills = list(map(int, input().split())).

Moreover, you must clear this list before each new query.

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Oooooooh ok I see I see. Appreciate the insight