python - How do you iterate numbers in a list? -
suppose i'd print numbers i've given inputs.
here program, want iterate numbers i've given input using array
. how do it?
array = [] num = input("enter number of element: \n") num = int(num) array = [num] print("enter the", num,"element: ") in range(0, num): mynum = input() mynum = int(mynum) array += [mynum] j in range(array[i]): print(j)
when run this, says:
//output: enter number of element: 3 enter 3 element: 5 6 4 0 1 2 3 4 5
what's wrong here?
edited
here new program:
array = [] num = input("enter number of element: \n") num = int(num) array = [num] print("enter the", num,"element: ") in range(0, num): mynum = input() mynum = int(mynum) array = [mynum] j in array: print(j)
//output
enter number of element: 3 enter 3 element: 4 5 6 6
in new program initialize list array
1 element contains number of loops. don't think want value in list.
in loop keep overriding list again , again. have append
data list.
then might want print message "enter x. element" in loop, not before.
num = int(input("enter number of elements: ")) array = [] in range(num): print('enter {}. element: '.format(i)) array.append(int(input()) j in array: print(j)
Comments
Post a Comment