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<0 or 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
n =[]
????
//This loop is for calling fibi function and printing its output on each diff line
for i in n:
print (fibi(n[i]))
1
2
3
4
5
.
.
.
n
1
1
2
3
5
Using split() Method The split() method is useful for getting multiple inputs from users. The syntax is given below. The separator parameter breaks the input by the specified separator. By default, whitespace is the specified separator.
Using Split () Function With the help of the split () function, developers can easily collect multiple inputs in Python from the user and assign all the inputs to the respective variables. Developers can specify a character that will be used as a separator to break the input provided by the user.
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])
from sys import stdin
lines = stdin.read().splitlines()
print(lines)
INPUT
0
1
5
12
22
1424
..
...
OUTPUT
['0', '1', '5', '12', '22', '1424' .. ...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With