How to randomly call any method from a list

user3346746

Am still new to web2py and python, in my web2py app, i created this code that works well in python shell.

python modules: The methods work in such a way that a user inputs an equation query to get an answer. If it is an addition, method1 works it out, the same to other methods being invoked to performing different codes e.g.

def method1():# to do additions
    name = input('Please Enter equation here: ').lower()
    if '1 + 1':
        answer = code
        return answer

def method2():# to do subtractions
    name = input('Please Enter equation here: ').lower()
    if '1 - 1':
        answer = code
        return answer

In the controller, I imported the methods as follows though there are many more methods than these shown

from applications ...... import method1
from applications ...... import method2
from applications ...... import method3
from applications ...... import method4

method1 = method1
method1 = method2
method1 = method3
method1 = method4

G0 = [method1, method2, method3, method4]

def Foo():
    code..
    for (func) in G0:
        return func()

The problem is that only method1 which is at position[0] in the list is invoked and not other methods. I want to randomly call any method when a user inputs any query.

Padraic Cunningham

If you want to call methods randomly use random.choice:

def foo1():
    print "hello"

def foo2():
     print "world"

def foo3():
     print "goodbye"

def foo4():
     print "world"
GO = [foo1, foo2, foo3, foo4]

import random

def Foo():
    func = random.choice(GO)
    return func()
In [30]: Foo()
world

In [31]: Foo()
goodbye

In [32]: Foo()
hello

In [33]: Foo()
goodbye

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call any method or property from user input

From Dev

How to call any method or property from user input

From Dev

How to use main method to randomly generate a day in a month ? Any ideas?

From Dev

How to call java method taking parameter as List<Class<?>> from Scala

From Dev

how to create a method that will call URL's From a list with flutter

From Dev

C# How to call array list item from outside method

From Java

How to randomly select an item from a list?

From Dev

How to test that an element is randomly selected from a list?

From Dev

Call to local method from list comprehension failing

From Dev

How to call a method with List<Child>, but method's parameter is List<Parent>?

From Dev

How to call a function from a list?

From Dev

How to call a function from any class by a pointer to it?

From Dev

How to select randomly from a list of string-array

From Dev

how to randomly select a certain number of elements from a list

From Dev

How to replace values in a column randomly using values from a list on Pandas?

From Dev

How can I remove a randomly chosen element from a linked list?

From Dev

How do I randomly select a variable from a list, and then modify it in python?

From Dev

How to replace values in a column randomly using values from a list on Pandas?

From Dev

Excel: How to extract repeated values from randomly ordered list?

From Dev

How to randomly pick value from a list in Unity3D?

From Dev

Is there any way to search for, and then call a method in java from a String?

From Dev

How to call functions randomly.

From Dev

Call parent method from child method using various argument list

From Dev

How do I call the method of a derived class from a list that contains parent objects?

From Dev

Get an item randomly from a list

From Dev

How to call object method for any object in my metaclass?

From Dev

Call a method from a method

From Dev

How to call object's method from Thymeleaf?

From Dev

How to call a method from a different blueprint in Flask?

Related Related

  1. 1

    How to call any method or property from user input

  2. 2

    How to call any method or property from user input

  3. 3

    How to use main method to randomly generate a day in a month ? Any ideas?

  4. 4

    How to call java method taking parameter as List<Class<?>> from Scala

  5. 5

    how to create a method that will call URL's From a list with flutter

  6. 6

    C# How to call array list item from outside method

  7. 7

    How to randomly select an item from a list?

  8. 8

    How to test that an element is randomly selected from a list?

  9. 9

    Call to local method from list comprehension failing

  10. 10

    How to call a method with List<Child>, but method's parameter is List<Parent>?

  11. 11

    How to call a function from a list?

  12. 12

    How to call a function from any class by a pointer to it?

  13. 13

    How to select randomly from a list of string-array

  14. 14

    how to randomly select a certain number of elements from a list

  15. 15

    How to replace values in a column randomly using values from a list on Pandas?

  16. 16

    How can I remove a randomly chosen element from a linked list?

  17. 17

    How do I randomly select a variable from a list, and then modify it in python?

  18. 18

    How to replace values in a column randomly using values from a list on Pandas?

  19. 19

    Excel: How to extract repeated values from randomly ordered list?

  20. 20

    How to randomly pick value from a list in Unity3D?

  21. 21

    Is there any way to search for, and then call a method in java from a String?

  22. 22

    How to call functions randomly.

  23. 23

    Call parent method from child method using various argument list

  24. 24

    How do I call the method of a derived class from a list that contains parent objects?

  25. 25

    Get an item randomly from a list

  26. 26

    How to call object method for any object in my metaclass?

  27. 27

    Call a method from a method

  28. 28

    How to call object's method from Thymeleaf?

  29. 29

    How to call a method from a different blueprint in Flask?

HotTag

Archive