user1000368 user1000368 - 4 months ago 27
Python Question

How to take input in an array + PYTHON?

Hi I am new to python and want to take input in an array. The python doc is not well described about arrays. Also I think I have some hiccups with the for loop in python.

I am giving the C code snippet which I want in python:

C Code:

printf("Enter howmany elements you want: ");
printf("Enter the numbers in the array: ")


raw_input is your helper here. From documentation -

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

So your code will basically look like this.

num_array = list()
num = raw_input("Enter how many elements you want:")
print 'Enter numbers in array: '
for i in range(int(num)):
    n = raw_input("num :")
print 'ARRAY: ',num_array

P.S: I have typed all this free hand. Syntax might be wrong but the methodology is correct. Also one thing to note is that, raw_input does not do any type checking, so you need to be careful...