0

How can I use for loop here?

Code:- a=int(input()) b=int(input()) list=list(range(a, b)) print(list) Input:-0 1 2 3 Example output:-[0,1,2,3]

10th Jan 2021, 3:45 PM
Priyankar Ghosh
Priyankar Ghosh - avatar
6 Réponses
+ 4
# If all inputs are always 4 lines. lst = [] for i in range(4): lst.append(int(input()) OR # Same thing using list comprehension lst = [int(input()) i for i in range(4)] - - - - - - - - - - - - - - - - - - - - - - - - - - OR # If your input depends on how many input lines (variable "a") a = int(input()) lst = [] for i in range(a): lst.append(int(input()) print(lst) OR # Same thing using list comprehension a = int(input()) lst = [int(input()) for i in range(a)]
10th Jan 2021, 4:02 PM
noteve
noteve - avatar
+ 2
Priyankar Ghosh I dont think that will work on every input cause for example: INPUT: 0 1 2 3 - - - - - - - - - - - # Since you have two input variables (a and b), then you are getting two lines of input. 0 ---> a 1 ---> b 2 ---| 3 ---| As you can see here, your program only takes the first two lines of your inputs (0 and 1) and 2 and 3 are ignored because your program has no input variables left. And therefore your output will look like this: >> [0]
11th Jan 2021, 9:41 AM
noteve
noteve - avatar
+ 1
How can you use a for loop to do what?
10th Jan 2021, 9:29 PM
Aron Mikes
Aron Mikes - avatar
+ 1
Here is for loop solution:- a=int(input()) b=int(input()) array = [] for d in range(a, b): array.append(d) print(array)
11th Jan 2021, 9:34 AM
Priyankar Ghosh
Priyankar Ghosh - avatar
0
Aron Mikes check my code,I made it without for loop...bt we can use for loop to get same output as I example there🙂
10th Jan 2021, 9:46 PM
Priyankar Ghosh
Priyankar Ghosh - avatar
0
《 Nicko12 》 a and b is a range just run my code u will clear that output comes in series within third bracket 🙂. Output:-[0,1,2,3]
11th Jan 2021, 9:55 AM
Priyankar Ghosh
Priyankar Ghosh - avatar