How can I create a list which it's range is determined by input? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How can I create a list which it's range is determined by input?

https://code.sololearn.com/cW4kzrl1Ro2G/?ref=app

8th Feb 2021, 11:24 AM
Xaniar
Xaniar - avatar
3 Answers
+ 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]
8th Feb 2021, 11:31 AM
Lothar
Lothar - avatar
+ 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])
8th Feb 2021, 3:51 PM
Lothar
Lothar - avatar
+ 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.
8th Feb 2021, 11:36 AM
#0009e7 [get]
#0009e7 [get] - avatar