I am new to python. I want to take user inputs of 2 integer arrays a and b of size 4 and print them. The input should be space seperated.
First user should input array a[] like this:
1 2 3 4
The he should input array b[] like this
2 3 4 6
The program should display a and b as output.I want the variables in a and b to be integers and not string.How do I this?
I was trying something like this
a=[]
b=[]
for i in range(0,4):
m=raw_input()
a.append(m)
for i in range(0,4):
n=int(raw_input())
b.append(n)
print a
print b
But this does not work.
Your program is working fine. You just didn't pass the
prompt string
which gets prompted on terminal to ask user's input:This is how it will result:
If you are expecting only
integers
as input you can useinput
built-in function, by which there is no need to type cast it again to integer.raw_input
reads a single line and returns it as a string.If you want to split the line on spaces a solution is
note that them will be arrays of strings, not of integers. If you want them to be integers you need to ask it with
or, more explicitly
The Python interactive shell is a great way to experiment on how this works...
raw_input()
reads a line from the user, that line needs to be splitted by spaceNext, You'll need to convert the data to
int
The easiest way to do that, is list comprehensionYou can use python's sys module for reading input from command line.
Here i have the complete code with example
INPUT
OUTPUT
From your description, I would code something like...