[Solved] How do I loop this? [closed]


You almost certainly want to replace your specific variables for each nation with a data structure (such as a dictionary) that uses the nation’s name as a key. So, rather than referring to USA_REGION_DATA you’d look up REGION_DATA["USA"]. This is extensible to any number of nations, as you can simply add new values to the dictionary.

You can initialize it with something like:

REGION_DATA = { "USA": {'Bordering':['MEXICO','CANADA'],'MILITARYGROWTHRATE':1.03},
                "MEXICO": {'Bordering':['USA'],'MILITARYGROWTHRATE':1.01},
                "CANADA": {'Bordering':['USA'],'MILITARYGROWTHRATE':1.01}
              }

Your attack function (and others) would be generic, with no special casing for individual nations:

def attack(origin,target):
    '''Origin is where you are attacking from,
    target is who you are attacking'''
    x=origin.upper()
    y=target.upper()
    if x not in REGION_DATA:
        print("You must attack from somewhere!")
    elif y not in REGION_DATA[x]['Bordering']:  # extra indexing by x here
        print("Not a valid target")
    else:
        print("Attack is underway!")

1

solved How do I loop this? [closed]