Reputation: 1053
I must be missing something here since the computer doesn't joke around but this simple for loop is seemingly not giving me the desired output. Below is the code which uses aztro's API to grab today's horoscope for each of 12 zodiac signs and put them all in a list.
import requests
import json
zodiacSigns = ['Aries', 'Taurus', 'Gemini', 'Cancer', 'Leo', 'Virgo', 'Libra', 'Scorpio', 'Sagittarius', 'Capricorn', 'Aquarius', 'Pisces']
for zodiacSign in zodiacSigns:
params = (('sign','{}'.format(zodiacSign)), ('day','today'))
output = json.loads(requests.post('https://aztro.sameerkumar.website/', params=params).text)
descriptions = []
descriptions.append(output['description'])
print(descriptions)
This code outputs the horoscope for only Pisces, the last element in the list above:
["You need to take work more seriously today -- it may be that you've got an opportunity coming up that shouldn't be missed. It's easier than usual for you to make career moves, so go for it!"]
As a reference, a sample output of this aztro's API for a single zodiac sign is:
{
"compatibility":" Virgo",
"date_range":"Jan 20 - Feb 18",
"current_date":"August 23, 2018",
"description":"Today requires a willingness to go deeper than usual -- maybe to explore the nuances of your primary relationship, maybe to really get to know that one client or maybe just reading between the lines.",
"lucky_time":" 10am",
"lucky_number":" 13",
"color":" Navy Blue",
"mood":" Thoughtful"
}
The desired output would be a list of horoscopes for all 12 zodiac signs. I can't seem to catch the issue here, so I'd appreciate input from more experienced eyes. Gracias!
Upvotes: 2
Views: 440
Reputation: 11929
The problem lies in the declaration of the descriptions
variable, which at every iteration is initialized to be an empty list.
Just move it out from the loop, like so:
descriptions = []
for zodiacSign in zodiacSigns:
params = (('sign','{}'.format(zodiacSign)), ('day','today'))
output = json.loads(requests.post('https://aztro.sameerkumar.website/', params=params).text)
descriptions.append(output['description'])
Upvotes: 4
Reputation: 308
The statement descriptions = []
should be out of the for loop. If it is in the for loop, it will be initiated (erased, in this case) every iteration.
The code below should work:
import requests
import json
zodiacSigns = ['Aries', 'Taurus', 'Gemini', 'Cancer', 'Leo', 'Virgo', 'Libra', 'Scorpio', 'Sagittarius', 'Capricorn', 'Aquarius', 'Pisces']
descriptions = []
for zodiacSign in zodiacSigns:
params = (('sign','{}'.format(zodiacSign)), ('day','today'))
output = json.loads(requests.post('https://aztro.sameerkumar.website/', params=params).text)
descriptions.append(output['description'])
print(descriptions)
Upvotes: 0