1

my code is like this

def quote():
    complete_url = 'https://type.fit/api/quotes'
    req = requests.get(complete_url)
    response= req.json()
    raw = []
    quote = []
    for i in range (len(response)):
        raw.append(response[i]['author'])
        unique_list = list(set(raw))
    author = random.choice(unique_list)
    search = input("what are you thinking about")
    for i in range (len(response)):
        for key, value in response[i].items():
            if str(search) in str(value):
                quote.append(value)
    a=len(quote)
    print(quote[randrange(a)])
    feedback = input('Do you like this(Y/N)')

and I tried to create a notepad to remember behavior of the users to read so that the software can know what to prioritize the next time by:

opening = open("learning.txt", "r")
behavior = []
for x in opening:
    #print(x)
    behavior.append(x[:-1]) if x[-1] == "\n" else behavior.append(x)
learn = list(set(behavior))
function_name = learn[1]

Because the variables have not been defined already, or the dynamic variables, so I cannot use getattr or locals() and globals() as like this link:[https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string][1]

So, how can I call the function?

  • `behavior.append(x[:-1]) if x[-1] == "\n" else behavior.append(x)` don't do that. Use a *conditional statement* not a pointless conditional expression... and really, that should probably just be `behavior.append(x.rstrip('\n'))` – juanpa.arrivillaga Jun 08 '21 at 14:38
  • Put `unique_list = list(set(raw))` *outside* the loop. And use `random.choice(quote)` – juanpa.arrivillaga Jun 08 '21 at 14:39
  • In any case, I'm not exactly sure what you are asking. What exactly are you trying to to that you cannot? It isn't clear. – juanpa.arrivillaga Jun 08 '21 at 14:40
  • I define some functions for my virtual assistant,it is our task. Anyway, I now have a list of function like: quote, open browser automatically, answer the question about math &knowledge,... and now I need to call it by learning frequency of user's choice, for example, if user usually chooses quote functions, my AI will offer and call it for them – Ngọc Nguyễn Jun 09 '21 at 00:00

0 Answers0