0

I have written a code that should input numbers from the user and report back the numbers from 1 to 100 that are missing from their input.

My code is below which doesn't work:

num_list = []

number = input('Enter numbers (remember a space): ')

number.split()

num_list.append(number)

for i in range(1, 101):
  if i in num_list:
    continue
  else:
    print(i, end =', ')

The code outputs all the numbers from 1 to 100 but doesn't exclude the numbers.

Note: The code has to exclude all the numbers entered not only one number.

E.g. if the user inputted 1 2 3 4 the output should start from 5 and list the numbers through to 100.

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
James
  • 35
  • 4
  • Possible duplicate of [Why doesn't calling a Python string method do anything unless you assign its output?](https://stackoverflow.com/questions/9189172/why-doesnt-calling-a-python-string-method-do-anything-unless-you-assign-its-out) – Davis Herring Jun 08 '19 at 03:34
  • 1
    This is a great time to learn how to debug. Try printing out the values of variables at various points in the code and make sure it matches your expectations. – eesiraed Jun 08 '19 at 03:36
  • It would be better to use a `set` rather than a `list` if you're only using it to test for membership. – Barmar Jun 08 '19 at 03:51

1 Answers1

2

There are three of problems

1) your are not saving the returned list from split method

result = number.split()

2) Use extend instead of append

num_list.extend(result)

3) By default input will read everything as string, you need to convert them into int from string after splitting, below is example using List Comprehensions

result = [int(x) for x in number.split()]

append : Will just add an item to the end of the list

So in you case after appending user input your list will be

num_list.append(number) #[[1,2,3,4,5]] so use extend

extend : Extend the list by appending all the items from the iterable.

num_list.append(number) #[1,2,3,4,5] 

Note : If the num_list empty you can directly use result from split method, no need of extend

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • 1
    There's no reason to use `extend` with an empty list. Just assign the result of splitting directly to `num_list`. – Barmar Jun 08 '19 at 03:51