Integer array input in Python 2

I am new to python. I want to take custom inputs from 2 integer arrays a and b of size 4 and print them. The input must be separated by a space.

The first user should enter the array a [] as follows:

1 2 3 4

It should enter an array b [] like this

2 3 4 6

The program should display a and b as output. I want the variables in and b to be integers, not string. How am I?

I tried 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 that won't work.

+3


source to share


6 answers


raw_input

reads one line and returns it as a string.

If you want to split the string into spaces the solution would be

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

      

note that these will be arrays of strings , not integers. If you want them to be integers you need to set

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))

      

Python's interactive shell is a great way to experiment with how it 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]
>>> _

      

+3


source


raw_input()

reads a line from the user, this line must be separated by a space

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

      



Next, you will need to convert the data to int

The easiest way to do this is a list comprehension

a = [int(x) for x in a]
b = [int(x) for x in b]

      

+3


source


Your program is working fine. You just didn't pass prompt string

which one is being requested on the terminal to query the user:

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

      

Here's how it goes:

 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 only expecting input integers

, you can use a built-in function input

where you don't need to type it again into an 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)).

      

+1


source


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

      

0


source


You can use the sys python module to read input from the command line.

import sys;
a = map(int, sys.stdin.readline().split());
b = map(int, sys.stdin.readline().split());
print a;
print b;

      

0


source


Here I have complete code with example

n=int(raw_input())
a=map(int,raw_input().split())
m=0
for i in range(n):
    print a[i]

      

ENTRANCE

6

2 3 9 10 12 15

OUTPUT

2

3

nine

ten

12

15

0


source







All Articles