How do I add to a variable based on another variables value? (Python)

Unnamed

So I am making a game in Python, and have this bit of code as a function.

def playerDeploySoldiers():
    global error #a basic error, contect the creator message
    answer=1#tells the program when to stop asking
    while(answer==1):
        clearScreen()#just clears the screen
        printDeploySoldiers()
        #needed to define answerDeploySoldiers
        if(answerDeploySoldiers=="1"):
            deploySpot=sanitised_input("Where: ", str, range_=(playerCountries))
            deployNumber=sanitised_input("How many: ", int, 1, playerExtraSoldiers)
        elif(answerDeploySoldiers=="0"):#none of this matters
            print("You have finished deploying soldiers")
            answer=0
        else:
            print(error)

sanitised_input() is just input() but ensuring the player can't add something random like 'asdflasdf;jasdf' as their input. deploySpot will be equal to a country name. For every country, there is a variable called armiesCountryNameHere. deployNumber is a positive integer.

I want to add deployNumber, an integer value, to armiesValueOfdeploySpot (armiesdploySpot+=deployNumber), without having to put an if-statement for every possible country deploySpot could be. Is this possible? For example, if deploySpot==USA (not actually an option but just to pretend), I would add the value of deployNumber (which could be something like 100 or 986) to armiesUSA (armiesUSA+=deployNumber). But I can't be sure that deploySpot==USA. It could be Mexico, Brazil, Russia, France, (none of these are actual options but they're examples), or any other country.

I have tried finding answers on stackoverflow, but came up with nothing I could understand or ever hope to with the keywords or similar questions I could think of. Most things involved creating a variable using another's value, using a differet language for something different, or something completely beyound my understanding.

For clarification, I am trying to avoid having to do this under deployNumber's value assignment.

if(deployNumber=="USA"):
    armiesUSA+=deployNumber

and having to do that for EVERY possible deploySpot value.

MATOS

Dictionaries in python can be assigned like so:

First_var = 0
Second_var = "hi"
MY_DICT = {First_var:Second_var}
# Reference like so:
Value = MY_DICT[First_var]
#Value now equals Second_var because it assigns the value of the first variable to the next.

I think this is what you are asking? If you mean that you want the value to change based on another variable then add:

CONDITION = True
if(CONDITION):
   MY_DICT[First_var] = NewValue

I believe this should change the value assigned. Tell me if I am wrong

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

R: How do I choose which row dplyr::distinct() keeps based on a value in another variable?

In Python, how to set value for one __init__ variable based on another?

How do I create a variable that increments by 1 based on the value of another variable?

How do I add a value next to another value in dict?

How can I set a runtime environment variable based on the value of another

How do I conditionally change the value of a variable based on the value of another variable?

In Google Sheets, how do I filter based on the value of another cell?

How do I select column based on value in another column with dplyr?

How do I get the value of a variable whose name is based on another variable?

How do I get a table value based on the value of another string?

How to add (sum) the two values in a python dictionary based on another value?

How do i set a temp variable to a value based from another table

How to assign a value to the variable based on the existing variables in python?

How do I add a variable's number to another variable in batch?

How do I add or amend a node based on another XML file?

How do I add a new attribute to each ActiveRecord in array based on value from another array?

How do I add a value saved in a file to another variable?

in R, how do I have the scatterplot choose a color for a point based on the value of another variable?

How do I add variables to a directory in python

How do I add a field that is based on another in a query

How do I calculate second largest value and add another column for it in Pandas Python

How do I add a statement in a variable when part of the statement is variable and the variables value is too be assigned?

How do I set a cell value based on another cell in same row in Python Using Pandas

How do I set the value of a variable based on another variable?

How do I parse for a dynamic variable based on constant variables in shell?

How do I update a variable value in python?

How do I change a value within a variable based on the value of another variable in R?

How do I add the value from one dataframe based on a column to another dataframe based on a row?

How do I replace the value of an observation based on another variable's values, within a group?