Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

raw_input reads a single line and returns it as a string.

If you want to split the line on spaces a solution is

a = raw_input().split()
b = raw_input().split()

note that them will be arrays of strings, not of integers. If you want them to be integers you need to ask it with

a = map(int, raw_input().split())
b = map(int, raw_input().split())

or, more explicitly

a = []
for x in raw_input().split():
    a.append(int(x))
b = []
for x in raw_input().split():
    b.append(int(x))

The Python interactive shell is a great way to experiment on how this works...

Python 2.7.8 (default, Sep 24 2014, 18:26:21) 
[GCC 4.9.1 20140903 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "19 22 3 91".split()                                                        
['19', '22', '3', '91']
>>> map(int, "19 22 3 71".split())                                              
[19, 22, 3, 71]
>>> _
share|improve this answer
    
Thank you sir for your answer. Can you please tell me exactly where i should place the "for i in range(0,4):" for taking the values of 4 space seperated inputs from the user and what parts should be enclosed within the loop –  user220789 Dec 28 '14 at 9:24
    
@user220789: str.split() already does the needed loop internally and returns the list containing the spaces-separated parts; so raw_input.split() is already a list (but it's a list of substrings, not a list of ints). –  6502 Dec 28 '14 at 9:27
    
So basically what happens here is that after I hit enter after the inputs, the new loop " for x in raw_input().split(): b.append(int(x))" starts?So according to my understanding no checking for upper limit of array size occurs here I can enter any number of integers for a. Only when I hit enter the new list b begins. –  user220789 Dec 28 '14 at 9:36
    
@user220789: nothing happens when you hit space. Python raw_input always waits for you typing enter and then returns a string containing what you typed (including spaces): always a single string like e.g. "1 2 3 4". Calling split() on that string returns ["1", "2", "3", "4"], i.e. a list of strings and then you need to convert them to integers. This can be done compactly with map(int, ...) or explicitly like in the two loops I've shown. –  6502 Dec 28 '14 at 9:40
    
that was a typo, Sir I meant enter only. I have corrected my comment now.Thank you for your help, Sir. –  user220789 Dec 28 '14 at 9:42

raw_input() reads a line from the user, that line needs to be splitted by space

a = raw_input().split()
b = raw_input().split()

Next, You'll need to convert the data to int The easiest way to do that, is list comprehension

a = [int(x) for x in a]
b = [int(x) for x in b]
share|improve this answer
1  
or a = map(int, a) –  6502 Dec 28 '14 at 9:11

Your program is working fine. You just didn't pass the prompt string which gets prompted on terminal to ask user's input:

a=[]
b=[]
for i in range(0,4):
    m=int(raw_input(" Enter value for a list :"))
    a.append(m)
for i in range(0,4):
    n=int(raw_input(" Enter value for b list :"))
    b.append(n)

print "list a looks like :-", a
print "list b looks like :-", b

This is how it will result:

 Enter value for a list :1
 Enter value for a list :2
 Enter value for a list :3
 Enter value for a list :4
 Enter value for b list :5
 Enter value for b list :6
 Enter value for b list :7
 Enter value for b list :8
list a looks like :- [1, 2, 3, 4]
list b looks like :- [5, 6, 7, 8]

raw_input(...)
    raw_input([prompt]) -> string

    Read a string from standard input.  The trailing newline is stripped.
    If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
    On Unix, GNU readline is used if enabled.  The prompt string, if given,
    is printed without a trailing newline before reading.

If you are expecting only integers as input you can use input built-in function, by which there is no need to type cast it again to integer.

a=[]
b=[]
for i in range(0,4):
    m = input(" Enter value for a list :")
    a.append(m)
for i in range(0,4):
    n = input(" Enter value for b list :")
    b.append(n)


input(...)
    input([prompt]) -> value

Equivalent to eval(raw_input(prompt)).
share|improve this answer

From your description, I would code something like...

def foo():
    a = raw_input()
    a = a.split()
    a = [int(x) for x in a]
    if len(a) != 4:
            raise Exception("error: input 4 integers")
    b = raw_input()
    b = b.split()
    b = [int(x) for x in b]
    if len(b) != 4:
            raise Exception("error: input 4 integers")
    print a
    print b
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.