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
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
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)
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"