0

I am creating a sequence identifier and I want a shorter code by manipulating the list. However:

#This is possible:
list = [1,2,3,4,5]
A = list[1] - list[0]
print(A)

#This is not possible:
list = [input()]
A = list[1] - list[0]
print(A)
  • 2
    The result of `input()` is a _string_; you can either use some expression to convert it to a list of integers `[int(x) for x in input().split(",")]`or `eval()` valid Python (though [beware `eval` is very dangerous](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice)) – ti7 Dec 01 '21 at 16:33
  • **list** is a built-in type, you should not use it as a variable name, although python allows you to do. – Guinther Kovalski Dec 01 '21 at 16:39

1 Answers1

1

[input()] is only one (string) element, so list[1] doesn't exist to be used in subtraction.

If you want a list of 5 user-entered integers, you would use

[int(input()) for _ in range(5)]

And press enter after each value

Or for space-separated values - [int(x) for x in input().split()]

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245