-4

I have multiple functions in order like below:

  test1func()
  test2func()
  test3func()
  test4func()
  test5func()
  test6func()

I want to develop a logic which asks the user to input the number, from where the program will executing the functions.

For example if user selects 3 then it should start from function test3func() to test6func() if he selects 5 then test5() then from test5func() to test6func().

Trial code:

 print "1).fun1\n2).fun2\n3).fun3\n4).fun4\n5).fun5\n6).fun6"
 select_fun = raw_input"Choose from which function it has to start" 

How can I achieve above requirement?

gaya
  • 463
  • 2
  • 6
  • 22
Bittu
  • 31
  • 2
  • 9

4 Answers4

3
def t1():
    print(1)

def t2():
    print(2)

def t3():
    print(3)

def t4():
    print(4)

def t5():
    print(5)

def t6():
    print(6)

# put the functions in a list
functions = [t1, t2, t3, t4, t5, t6]

def go(functions):
    # .format(variable) replaces {} with the value of variable (string formatting)
    number_of_funcs = len(functions)
    greeting = "Type a number from 1 to {}: ".format(number_of_funcs)
    selected = input(greeting)
    try:
        # index to start from
        index = int(selected) - 1
    except ValueError:
        # check if the user wrote a number (exception handling)
        print('Invalid input. Not a number')
        return
    if index > number_of_funcs - 1 or index < 0:
        msg = 'Invalid input. Consider a number from 1 to {}'.format(number_of_funcs)
        print(msg)
        return
    # iterate through the functions of the list
    # starting from the index specified (list slicing)
    for f in functions[index:]:
        f()

while(True):
    # infinite loop. press Ctrl+C to abort
    go(functions)

See also

exception handling

string formatting

list slicing

Maxim Krabov
  • 685
  • 5
  • 17
2

You can put all your functions in a list and call sequentially all the functions that are contained in a slice of the list, as in this toy example

In [11]: l_of_functions = [ lambda x=x: print(x) for x in range(10)]

In [12]: for fun in l_of_functions[5:]: fun()
5
6
7
8
9

In [13]: for fun in l_of_functions[0:]: fun()

0
1
2
3
4
5
6
7
8
9

In [14]:

Addendum

In case the OP needs a function to get a number from a closed interval, here it is my attempt

In [28]: def ask_inside(minimum, maximum, max_tries=10, this_try=0):
    ...:     answer = input('Give me a number comprised between %d and %d: '
    ...:                    %(minimum, maximum))
    ...:     try:
    ...:         number = int(answer)
    ...:     except ValueError:
    ...:         number = minimum-1
    ...:     if minimum <= number <= maximum: return number
    ...:     if this_try+1<max_tries:
    ...:         return ask(minimum, maximum,
    ...:                    max_tries=max_tries, this_try=this_try+1)
    ...:     else: print('You are boring')
    ...:     

In [29]: ask_inside(1, 6, max_tries=3)
Give me a number comprised between 1 and 6: 2
Out[29]: 2

In [30]: ask_inside(1, 6, max_tries=3)
Give me a number comprised between 1 and 6: ojig
Give me a number comprised between 1 and 6: 0 
Give me a number comprised between 1 and 6: 7
You are boring

In [31]: 

Of course if you are on Python 2 print is a statement and input()raw_input().

gboffi
  • 22,939
  • 8
  • 54
  • 85
1

I suppose that you could build a dictionary :

functions = {
        1: test1func,
        2: test2func,
        3: test3func,
        4: test4func,
        5: test5func,
        6: test6func
    }

And then call the function whose key was received as input : functions[select_fun]().

Edit :

As commented below, I misunderstood the task to perform. To run all functions, from the one 'inputed' to the last, do so :

for i in range(select_fun, 7):
    functions[i]()
Archeo
  • 221
  • 1
  • 6
0
user_input = int(raw_input('Enter number')) # or int(input('Enter number')) for python3
for i in range (user_input,7):
     locals()["test"+str(i)+"function"]() #or  globals()["test"+i+"function"]()

Use locals if your functions are local or globals if there are global.

Use raw_input for python 2.x and input for python 3.x

Alex Lucaci
  • 620
  • 8
  • 18
  • using `locals()` or `globals()` this way is considered bad style, in that it's not as explicit (and not as maintainable) as using a dedicated `dict` (or `list` or other container). Also it doesn't solve the OP's problem, please reread the question more carefully. – bruno desthuilliers Jun 27 '17 at 14:18