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]
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)]
+ 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]
+ 1
How can you use a for loop to do what?
+ 1
Here is for loop solution:-
a=int(input())
b=int(input())
array = []
for d in range(a, b):
array.append(d)
print(array)
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🙂
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]