0
Hey I want to write a code for taking user input in array what's the problem in my code ?
Import array as arr X = are.array('i', [ ]) Z = int(input('what length do u want: ')) i = 0 While i < (z): t = int(input('enter values: ')) i += 1 Print() arr.append(t) Print(t)
7 Answers
+ 5
In general the use of module arr is not necessary for the most use cases. It is used only very rarely. A list is also ok. A for loop with a range does the same as a while loop, but we don't need a counter variable (except you are forced to use while for some reasons). So a basic version of yout code could be:
z = int(input('what length do u want: '))
lst = []
for _ in range(z):
t = int(input('enter values: '))
lst.append(t)
print(lst)
# this is an other possible approach:
# this version does not need to get a separate number for items
lst = [int(i) for i in input('What length: ').split()] # enter like 1 4 2 ...
print(lst)
+ 3
Write first letter of while and print with lowercase
+ 3
You wrote While, Print it is wrong
You should write: while, print
+ 1
this is better
https://code.sololearn.com/cp8qEXE1KJ21/?ref=app
0
I don't understand Muhammadin
0
import array as arr
x = arr.array('i', [ ])
z = int(input('what length do u want: '))
i = 0
while i < (z):
t = int(input('enter values: '))
i += 1
x.append(t)
print(x)
This code should work...
There are some mistakes with capital letters, as mentioned before...
Moreover, you should append to list inside while loop...
And your array is instantiated as x, thus it must be called x, not arr - as arr is the class name...
0
You could also do
print(x.tolist())
in the last line, to print it as a simple list, which might be a little more readable...