How to take infinite input in python

Now, the thing is that I am supposed to take an unknown amount of input from the user like on one run he can enter 10 terms on another run he can enter 40. And I cannot ask user initially to enter the value of n so that I can run a range loop and start storing the input in list. If somehow I can do this then for that I have created the loop but that is not the case. So, the question is how to define the endpoint for user? or how to pass unknown number of arguments to the function?

def fibi[n]:
    while n=50:
        print "Enter value of n greater than 0 but less than 50"
        n = int[raw_input[]]
    if n==0:
        return n
    else:
        a, b = 0, 1
        for i in range[n]:
            a, b = b, a + b
    return a

main calling function starts

n =[]
????
//This loop is for calling fibi function and printing its output on each diff line
for i in n:
    print [fibi[n[i]]]

Sample Input:each entry should be on a new line

1
2
3
4
5
.
.
.
n

Sample Output

1
1
2
3
5

asked Jul 23, 2015 at 16:34

5

This is how to read many integer inputs from user:

inputs = []
while True:
    inp = raw_input[]
    if inp == "":
        break
    inputs.append[int[inp]]

If you want to pass unknow number of arguments to function, you can use *args:

def function[*args]:
    print args
function[1, 2, 3]

This would print [1, 2, 3].

Or you can just use list for that purpose:

def function[numbers]:
    ...
function[[1, 2, 3]]

answered Jul 23, 2015 at 16:49

Hannes KarppilaHannes Karppila

9292 gold badges14 silver badges31 bronze badges

4

from sys import stdin 
lines = stdin.read[].splitlines[]
print[lines]

INPUT

0
1
5
12
22
1424
..
...

OUTPUT

['0', '1', '5', '12', '22', '1424' .. ...]

answered Oct 7, 2018 at 17:58

1

In most cases, the error thrown is "EOFError : EOF while reading a line"

If we handle this error, our job is done!

while True:
    try:
        var = input[]
        # do your task 

    except EOF as e:
        pass

answered Oct 26, 2021 at 7:41

I am guessing that n is a list passed as command line arguments, if so then you can try doing

import sys

noOfArgs = len[sys.argv]
n = sys.argv

Here is a link that shows how to parse command line arguments.

answered Jul 23, 2015 at 16:47

l=[]
while[True]:
        try:
            a=input[]
        except Exception as e:
            break
        l.append[int[a]]
print[*l]

answered Dec 10, 2019 at 18:19

0

I am guessing this is what you were looking for

inputs = []
while True:
    sval = input["Enter a number: "]
    if sval == 'done' :
        break
    try:
        fval = float[sval]
    except:
        print['Invalid input']
        continue
    try:
        inputs.append[int[sval]]
    except:
        inputs.append[float[sval]]
print[inputs]

answered Jul 21, 2020 at 13:51

argv is a list of inputs passed to the script. the first argument argv[1] is the name of your script. what comes after that can be considered as passed arguments to the script. you can simply use the script below to get the inputs and store them in a list.

import sys
inputs = []
for item in sys.argv[1:]:
    inputs.append[item]
print[inputs]

answered Aug 9, 2021 at 2:19

p ghp gh

391 silver badge6 bronze badges

I think the best way to handle this problem is by using error handling i.e try and except block please refer to the code below.

while True:
try:
    n = input[]
    
    # Your Logic or code
    
except:
    break

answered Dec 31, 2021 at 14:24

How do you repeat input in Python?

There are two ways to do keep asking for user input in Python. First using while true with if statement and break statement. Another way is using a while loop with condition expression.

How do you input a list without length in Python?

“make user entered list in python without asking user list size” Code Answer's.
# number of elements..
n = int[input["Enter number of elements : "]].
# Below line read inputs from user using map[] function..
a = list[map[int,input["\nEnter the numbers : "]. strip[]. split[]]][:n].
print["\nList is - ", a].

How do I enter multiple inputs in Python?

However, Python provides the two methods that help us to take multiple values or input in one line..
# Taking multiple inputs in a single line..
# and type casting using list[] function..
x = list[map[int, input["Enter multiple values: "]. split[]]].
print["List of students: ", x].

How do you take input until EOF in Python?

Use the sys. stdin. readlines[] method to read user input until EOF. The readlines[] method will return a list containing the lines.

Chủ Đề