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)

2nd Sep 2020, 12:57 PM
PIYUSH PRAJAPATI
PIYUSH PRAJAPATI - avatar
7 Antworten
+ 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)
2nd Sep 2020, 1:33 PM
Lothar
Lothar - avatar
+ 3
Write first letter of while and print with lowercase
2nd Sep 2020, 1:01 PM
Muhammadamin
Muhammadamin - avatar
+ 3
You wrote While, Print it is wrong You should write: while, print
2nd Sep 2020, 1:03 PM
Muhammadamin
Muhammadamin - avatar
2nd Sep 2020, 1:48 PM
Shadoff
Shadoff - avatar
0
I don't understand Muhammadin
2nd Sep 2020, 1:02 PM
PIYUSH PRAJAPATI
PIYUSH PRAJAPATI - avatar
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...
2nd Sep 2020, 1:07 PM
G B
G B - avatar
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...
2nd Sep 2020, 1:09 PM
G B
G B - avatar