0

How can I avoid the stopiteration error? I am unaware of any functions or loops that the code may be iterating through.

import wolframalpha

client = wolframalpha.Client('My Key is Goes Here')
res = client.query('pi')

print next(res.results).text
K. W. Cooper
  • 313
  • 3
  • 12

2 Answers2

0

I've never used wolframalpha, but this looks like it should be

for result in res.results:
    print result.text

Edit: I've now installed wolframalpha and I see that that query (and the others I have tried) have no results in res.results. Instead, they have pods. You can iterate through pods as in the docs:

for pod in res.pods:
    print '{p.title}: {p.text}'.format(p=pod)
matiasg
  • 1,927
  • 2
  • 24
  • 37
0

Your use of next is what is creating the StopIteration error. Python's API for Wolframalpha is providing an iterator for you to loop over the results with (next just gets the "next" result instead of all of them)

You could trap the error with a try/except clause:

try:
    print next(res.results).text
except StopIteration:
    print "No results"
Gerrat
  • 28,863
  • 9
  • 73
  • 101
  • That doesn't really solve the problem though, because nothing will print then. – K. W. Cooper Mar 01 '16 at 22:22
  • If it printed before, it will after. Getting a StopIteration error when calling next means there is no `next` results. My solution stops this Exception from propagating (and you could print "no results" in the `except` clause if you like that better). – Gerrat Mar 01 '16 at 22:23