0

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.

3
  • 4
    Sounds like you need to learn about dictionaries. Commented Apr 2, 2022 at 18:11
  • I want to add deployNumber ... are you saying that you want to do armiesValueOfdeploySpot += deployNumber? ...... or, are you saying that you want to associate deployNumber with armiesValueOfdeploySpot? Commented Apr 2, 2022 at 18:26
  • armiesValueOfdeploySpot+=deployNumber Commented Apr 2, 2022 at 18:38

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

7 Comments

Sort of, if I made a dictionary containing all the countries and their armies, I would want to change the value of their armies. The problem would be that CONDITION would have about 12 different possible values/outcomes, as their are 12 countries in my game. To be honest, I don't really understand this answer.
I'm trying to understand what you are asking then, I feel like dictionaries could work very well because you can change the values rather easily...
Just use += to MY_DICT[First_var] = NewValue and it should work fine
Ok I think I may have found an answer, I am trying it now.
Ok I tried something, and now it is shorter,but still pretty long. But at least it's not crazy long. I made a dictionary with every country's name and armies, the long part will be making every variable for armiescountryname reset to its value in my dictionary.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.