0

I've got a bit of code here using Wolfram Alpha & Wikipedia & wxPython:

import wikipedia
import wx
import wolframalpha

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None,
            pos=wx.DefaultPosition, size=wx.Size(450, 100),
            style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION |
             wx.CLOSE_BOX | wx.CLIP_CHILDREN,
            title="PyDA by ACORD")
        panel = wx.Panel(self)
        my_sizer = wx.BoxSizer(wx.VERTICAL)
        lbl = wx.StaticText(panel,
        label="Hello, I am PyDA, the Python Digital Assistant. How can I help?")
        my_sizer.Add(lbl, 0, wx.ALL, 5)
        self.txt = wx.TextCtrl(panel, style=wx.TE_PROCESS_ENTER,size=(400,300))
        self.txt.SetFocus()
        self.txt.Bind(wx.EVT_TEXT_ENTER, self.OnEnter)
        my_sizer.Add(self.txt, 0, wx.ALL, 5)
        panel.SetSizer(my_sizer)
        self.Show()

    def OnEnter(self, event):
        input = self.txt.GetValue()        
        input = input.lower()
        try:
            #wolframalpha
            app_id = "8A6LA2-ELRHR92Y88"
            client = wolframalpha.client(app_id)
            result = client.query(input)
            answer = next(result.results).text
            print(answer)
        except:
            #wikipedia
            print(wikipedia.summary(input))

if __name__ == "__main__":
    app = wx.App(True)
    frame = MyFrame()
    app.MainLoop()

So I've got a problem. When I only import wolfram alpha (see code below) I can ask questions to Wolfram Alpha (for example "What is the meaning of life" or maths questions like "2+2") and it gives me an answer. Similarly, when I only import wikipedia module, I can search for things like "Tim Cahill" or "Django" on wikipedia and I'll get an output. But when I combine both modules with the wxPython module for the Gui, I can only get results from wikipedia.

Here's the wolframalpha only code:

import wolframalpha
import wikipedia

input = input("Question: ")
app_id = "8A6LA2-ELRHR92Y88" # App ID for Wolframalpha

client = wolframalpha.Client(app_id) # calls on app id

# Output
result = client.query(input) # See line 3
answer = next(result.results).text


print(answer)

Here's the wikipedia only code:

import wikipedia

while True: # this is in a loop so it can be repeated
    wikinput = input("Ok Pyda! ")
    # wikipedia.set_lang("es")    # changes the language of output  to Espanyol (Spanish) - # not needed
    print(wikipedia.summary(wikinput, sentences=2)) # For wikipedia queries. # Sentences limits how long the output will be

Any help would be greatly appreciated

  • 1
    Can you show a failing example? I'm not clear from your question exactly what is happening. – larsks Nov 13 '19 at 12:51
  • 1
    You are calling wolframalpha.client(app_id) but it should be Client, with a capital C. This is why you don't use catch-all exception blocks and ignore the exception. – FiddleStix Nov 13 '19 at 12:54
  • 1
    @FiddleStix is correct. For future reference, you should only catch errors that you expect to happen and are out of your control (connection issues, files not existing, etc.). A `except` without any exception class will catch everything, including programmer errors, a category of errors that should almost never be caught. – James Mchugh Nov 13 '19 at 13:07

0 Answers0