1

I'm looking to replace a variable with a parameter in a function

Basically, I want to replace 'country' with 'aus' in this function. headline_aus is a value predefined elsewhere, I just want to be able to replace the word 'country' with 'aus', so I can use the function with other countries (I've got a variable called headline_usa which I want to retrieve using the same code)

def print_stories(country):
    Headline1 = headline_country[0]
    print(Headline1)

print_stories('aus')

I've been recommended a dictionary, but essentially want to use it like this, tho idk if that's how dictionaies work:

def print_stories(country):

    d = {'aus' : country = 'aus',
         'usa' : country = 'usa',
         'france': country ='france'}
    
    for i in range(3):
        Headline[i+1] = headline_country[i]
        Image[i+1] = image_country[i]
        Description[i+1] = Description_country[i]
        pubDate[i+1] = pubDate_country[i]

For context, I have a webscraper that uses the .xml feeds of 3 different news sources from 3 different countries (aus, france and usa). This webscraper function outputs global variables, for aus:

headline_aus
description_aus
image_aus
pubDate_aus

For usa and france, the variables are followed by _usa and _france respectively.

I wish to create a function (print_stories()), that can access these variables through a parametised function. I want to feed print_stories() either aus, usa, or france, and it will give me headline_aus, headline_usa, or headline_france.

Sean
  • 57
  • 6

1 Answers1

0

If headline_aus is a global variable this will work:

def print_stories(country):
    headline_country = globals()[f"headline_{country}"]
    Headline1 = headline_country[0]
    # Repeat this for headline_country description_country
    #image_country pubDate_country
    print(Headline1)

print_stories('aus')

In my opinion, It is better to use dic:


country_headlines={"aus":{"headline":headline_aus,
                          "description":description_aus,
                          "image":image_aus,
                          "pubDate" :pubDate_aus},
"usa":{"headline":headline_usa,
                          "description":description_usa,
                          "image":image_usa,
                          "pubDate" :pubDate_usa},
"france":{"headline":headline_france,
                          "description":description_france,
                          "image":image_france,
                          "pubDate" :pubDate_france}

}
def print_stories(country):
    headline_country = country_headlines[country]
    Headline1 = headline_country[0]
    # Repeat this for headline_country description_country
    #image_country pubDate_country
    print(Headline1)

print_stories('aus')


Bibhav
  • 1,579
  • 1
  • 5
  • 18
  • I'll have to recode all the different variables then, including image, headline, description, ect ect – Sean Oct 14 '22 at 05:37
  • @Sean I have modified the answer. Yes, it is a little difficult to edit the code. In my opinion, working with `dict`. But you may choose any of the above methods. – Bibhav Oct 14 '22 at 06:03