Himanshu Himanshu - 3 months ago 4
Python Question

How to take input in single line in python and storing it into list

I am getting Runtime Error - NZEC while running this code online. I searched.. it is because of input format. Please help me out.

import math
a=input()
b=int(a)
e=list()
answer=1
for c in range(0,b):
d=input()
e.append(d)
for c in range(0,b):
g=e[c]
answer=math.fmod(float(float(answer) * float(g)),float((10**9)+7))
print(int(answer))

Answer

if you want to take the second and following inputs in a single line (I guess separated by spaces), you can do the following:

import math
a=input()
b=int(a)
answer=1

e = raw_input().split(" ")[:b]

for c in range(0,b):
    g=e[c]
    answer=math.fmod(float(float(answer) * float(g)),float((10**9)+7))
print(int(answer))

For instance you would type "3[enter]" then "5 66 77[enter]". Is that what you need?

EDIT: I guess this is some kind of programming exercise. But if needed you can even omit the first input, and request just a list of X numbers separated by spaces:

import math
answer=1

for g in raw_input().split(" "):
    answer=math.fmod(float(float(answer) * float(g)),float((10**9)+7))
print(int(answer))
Comments