0

I want to create a set of roll no. of students So I input the total no. of students and then input each roll no.

here is the code:

a=int(input())
s1=set()
for i in range(0,a):
  num=int(input())
  s1.add(num)

but when I run the code and input values I get this error

9

1 2 3 4 5 6 7 8 9

Traceback (most recent call last):
  File "C:\Users\vepul\eclipse-workspace\demo\dash.py", line 4, in <module>
    num=int(input())
ValueError: invalid literal for int() with base 10: '1 2 3 4 5 6 7 8 9'
Adi219
  • 4,712
  • 2
  • 20
  • 43

1 Answers1

3

You're asking your code to int() the string "1 2 3 4 5 6 7 8 9".

This won't work as there are spaces between the numbers, meaning it can't be cast to an integer.

When it asks for an input, you need to enter one integer, not all of them. It will loop round (10 times in this case) to ask for a number; each time, you need to enter an integer.

If you're hoping to add all 9 integers in one go, try this:

s1 = set(map(int, input().split())) ## Make sure you enter the integers space-separated
Adi219
  • 4,712
  • 2
  • 20
  • 43