This is my code

import wolframalpha

app_id = '876P8Q-R2PY95YEXY'

client = wolframalpha.Client(app_id)

res = client.query(input('Question: '))

print(next(res.results).text)

the question I tried was 1 + 1 and i run it and then i get this error

Traceback (most recent call last):
  File "c:/Users/akshi/Desktop/Xander/Untitled.py", line 9, in <module>
    print(next(res.results).text)
  File "C:\Users\akshi\AppData\Local\Programs\Python\Python38\lib\site-packages\wolframalpha\__init__.py", line 166, in text
    return next(iter(self.subpod)).plaintext
ValueError: dictionary update sequence element #0 has length 1; 2 is required

Please help me

1

There are 1 best solutions below

0
On

I was getting the same error when I tried to run the same code.

You can refer to "Implementing Wolfram Alpha Search" section of this website for better understanding of how the result was extracted from the dictionary returned.

https://medium.com/@salisuwy/build-an-ai-assistant-with-wolfram-alpha-and-wikipedia-in-python-d9bc8ac838fe

Also, I tried the following code by referring to the above website....hope it might help you :)

import wolframalpha
client = wolframalpha.Client('<your app_id>')
query = str(input('Question: '))
res = client.query(query)
if res['@success']=='true':
    pod0=res['pod'][0]['subpod']['plaintext']
    print(pod0)
    pod1=res['pod'][1]
    if (('definition' in pod1['@title'].lower()) or ('result' in  pod1['@title'].lower()) or (pod1.get('@primary','false') == 'true')):
        result = pod1['subpod']['plaintext']
        print(result)
else:
    print("No answer returned")