+ 2
How can I create a list which it's range is determined by input?
3 Antworten
+ 11
Xaniar , not clear what you are asking, because you have attached a code that is doing what you mentioned. the only issue is, that the last number from the range is missing. so using 10 as input, the last number will be 9. also a thing that can be changed is the starting number, which is 0 by default. if you use 2 arguments for range, the first one is the starting number, including this number.
you can change your code:
N = int(input())
numbers = list(range(1, N +1))
print(numbers)
if input is 10, output will be [1,2,...,9,10]
+ 6
get , you are right. for "dynamic" input you can use this:
inp = input().split() # eg input 1 3 6 0 5 4 7 2
print([int(num) for num in inp if int(num) % 2 == 0])
+ 3
If you want user to write number of elements before elements, then Lothar is right.
If you want some type of "dynamic array input", you can use:
1. `list_input = list(map(float, input().split()))` (for "1 2 4 8")
2. `list_input = list(map(float, input().split(', ')))` (for "1, 2, 4, 8")
3. ```
list_input = []
element_input = input()
while element_input != '':
list_input.append(float(element_input))
element_input = input()
``` (for """1
2
4
8"""; will not work in SoloLearn Python Playground unless end of emenents input is something like "[end]")
Also, you can type "int" instead of "float" if you want to accept integers.