I have made a program that does the rolling for you for the board game "Risk" I even made it tell only the attackers top two rolled and it says the attacks and defenders rolled in order of highest to lowest.

But I need it to say who has to give up which pieces.

The rules in risk, in case the person who can help me doesn't know, is that you compare your highest role to their highest role and your second with their second. If both your roles are higher, they lose two guys, if one of yours is higher and one of theirs is higher you each lose one, ties are given to the defender.

#Risk dice simulator#

import random
from time import time

attack = True
defense = True

raw_input("Risk Rolling Simulator")

aR1 = random.randint(1,6)
aR2 = random.randint(1,6)
aR3 = random.randint(1,6)

dR1 = random.randint(1,6)
dR2 = random.randint(1,6)

print "Rolled:\n"
print "Attack:"
print aR1, aR2, aR3
print "\nDefense:"
print dR1, dR2

if dR1 > dR2:
    defense = dR1, dR2
elif dR1 < dR2:
    defense = dR2, dR1
elif dR1 == dR2:
    defense = dR1, dR2
    

if aR1 > aR3 and aR2 > aR3 and aR1 > aR2:
    attack = aR1, aR2
        
elif aR1 > aR3 and aR2 > aR3 and aR2 > aR1:
    attack = aR2, aR1
    
elif aR1 > aR2 and aR3 > aR2 and aR1 > aR3:
    attack = aR1, aR3
    
elif aR1 > aR2 and aR3 > aR2 and aR3 > aR1:
    attack = aR3, aR1
    
elif aR2 > aR1 and aR3 > aR1 and aR2 > aR3:
    attack = aR2, aR3
    
elif aR2 > aR1 and aR3 > aR1 and aR3 > aR2:
    attack = aR3, aR2

elif aR1 > aR3 and aR2 == aR3:
    attack = aR1, aR2
elif aR2 > aR3 and aR1 == aR3:
    attack = aR2, aR1
elif aR3 > aR1 and aR2 == aR1:
    attack = aR3, aR2
elif aR1 < aR3 and aR2 == aR3:
    attack = aR3, aR2
elif aR2 < aR3 and aR1 == aR3:
    attack = aR3, aR1
elif aR3 < aR1 and aR2 == aR1:
    attack = aR1, aR2
elif aR1 == aR2 and aR2 == aR3:
    attack = aR1, aR2

    
print "\nResults are:"
print attack, "vs.", defense

raw_input("")
execfile("Risk.py")

Hello there,

i've been playing around and this seems to have the right result for me:

AttackerWins = 0
DefenderWins = 0

if HighestAttackerRoll > HighestDefenderRoll:
    AttackerWins += 1

else:
    DefenderWins += 1

if SecondHighestAttackerRoll > SecondHighestDefenderRoll:
    AttackerWins += 1

else:
    DefenderWins += 1

print "\nOutcome:"
print "Attacker Wins %i" % (AttackerWins)
print "Defender Wins %i" % (DefenderWins)

the result with attack rolls 3, 2 vs defenders rolls 6 and 5:

Outcome:
Attacker Wins 0
Defender Wins 2

i've made some other changes if you want to me to show you feel free to ask, and i'll explain, but thats an answer to the problem :)

happy coding

How do I put this into the code? I'm sorry I'm a noob I know :P

hey no probs i always find it hard understanding other peoples code.

basically, you want to work out who wins right?

well if i'm right, then all you need to do is put that if, else, if, else section of my code into yours, however you will need to change some of the stuff so it works with your code.

to simplify it:

you have a tuple called attack and a tuple called defense right? each contains two numbers, the first being the higher dice roll and the second being the lower dice roll.
To work out the results you need to compare the first number of the attacker against the first number of the defender. If the attackers number is higher than the defenders number then the attacker gains a point. (or in the case of risk, the loser loses a unit) however if not, then the defender gains the point (again in the case of risk; loser loses a unit)
In Code:

if attack[0] > defense[0]:
    attackerWins += 1
else:
    defenseWins += 1

The second if, else combo does the same thing except it compares the two lower numbers.
In Code:

if attack[1] > defense[1]:
    attackWins += 1
else:
    defenseWins += 1

then at the end of it all, i simply print the results:

print attackWins
print defenseWins

if your still stuck let me know, glad to help

happy coding

I actually found my own way around it while I was waiting for a reply. I know many will look at my code and laugh at how pathetic it is. Because I did it by running through every possible scenario, so my code is not as neat as it could be and is way bigger than it has to be, but meh. I did manage to make the program much more elaborate once I got pased my first problem, but now I have a much harder problem.

#Risk dice simulator#

print "Risk Rolling Simulator"
attackForce = input("\nEnter the number of attacking units. ")
defenseForce = input("Enter the number of defending units. ")

import random
from time import time
loop = True

while loop:

    attack = 0
    defense = 0
    attackW = 0
    defenseW = 0
    men = "men."
    men2 = "men."

    print "Attacker has", attackForce,". While defender has", defenseForce,"."
    aR1 = random.randint(1,6)
    aR2 = random.randint(1,6)
    aR3 = random.randint(1,6)
    
    dR1 = random.randint(1,6)
    dR2 = random.randint(1,6)
    
    print "Rolled:\n"
    print "Attack:"
    print aR1, aR2, aR3
    print "\nDefense:"
    print dR1, dR2
    
    if dR1 > dR2:
        defense = dR1, dR2
        dH = dR1
        dL = dR2
    elif dR1 < dR2:
        defense = dR2, dR1
        dH = dR2
        dL = dR1
    elif dR1 == dR2:
        defense = dR1, dR2
        dH = dR1
        dL = dR2
    

    if aR1 > aR3 and aR2 > aR3 and aR1 > aR2:
        attack = aR1, aR2
        aH = aR1
        aL = aR2
    elif aR1 > aR3 and aR2 > aR3 and aR2 > aR1:
        attack = aR2, aR1
        aH = aR2
        aL = aR1
    elif aR1 > aR2 and aR3 > aR2 and aR1 > aR3:
        attack = aR1, aR3
        aH = aR1
        aL = aR3
    elif aR1 > aR2 and aR3 > aR2 and aR3 > aR1:
        attack = aR3, aR1
        aH = aR3
        aL = aR1
    elif aR2 > aR1 and aR3 > aR1 and aR2 > aR3:
        attack = aR2, aR3
        aH = aR2
        aL = aR3
    elif aR2 > aR1 and aR3 > aR1 and aR3 > aR2:
        attack = aR3, aR2
        aH = aR3
        aL = aR2
    elif aR1 > aR3 and aR2 == aR3:
        attack = aR1, aR2
        aH = aR1
        aL = aR2
    elif aR2 > aR3 and aR1 == aR3:
        attack = aR2, aR1
        aH = aR2
        aL = aR1
    elif aR3 > aR1 and aR2 == aR1:
        attack = aR3, aR2
        aH = aR3
        aL = aR2
    elif aR1 < aR3 and aR2 == aR3:
        attack = aR3, aR2
        aH = aR3
        aL = aR2
    elif aR2 < aR3 and aR1 == aR3:
        attack = aR3, aR1
        aH = aR3
        aL = aR1
    elif aR3 < aR1 and aR2 == aR1:
        attack = aR1, aR2
        aH = aR1
        aL = aR2
    elif aR1 == aR2 and aR2 == aR3:
        attack = aR1, aR2
        aH = aR1
        aL = aR2

    if aH > dH and aL > dL:
        attackW = 2
    elif aH > dH and aL < dL:
        attackW = 1
        defenseW = 1
    elif aH > dH and aL == dL:
        attackW = 1
        defenseW = 1
    elif aH < dH and aL > dL:
        attackW = 1
        defenseW = 1
    elif aH == dH and aL > dL:
        attackW = 1
        defenseW = 1
    elif aH < dH and aL < dL:
        defenseW = 2
    elif aH == dH and aL < dL:
        defenseW = 2
    elif aH == dH and aL == dL:
        defenseW = 2
    elif aH < dH and aL == dL:
        defenseW = 2

    if attackW == 1:
        men = "man."
    if defenseW == 1:
        men2 = "man."

    if attackW == 2:
        defenseForce -= 2
    elif defenseW == 2:
        attackForce -= 2
    else:
        defenseForce -= 1
        attackForce -= 1
    
    
    print "\nResults are:"
    print attack, "vs.", defense
    print "\nAttacker looses", defenseW, men2
    print "Defender looses", attackW, men
    print "Attacker now has", attackForce,". While defender has", defenseForce,"."

    if attackForce == 1:
        Fmen2 = "man"
    else:
        Fmen2 = "men"
    if defenseForce == 1:
        Fmen = "man"
    else:
        Fmen = "men"
    
    if attackForce == 0 or attackForce < 0:
        print "The defender wins with", defenseForce, Fmen, "remaining."
        break
    elif defenseForce == 0 or defenseForce < 0:
        print "The attacker wins with", attackForce, Fmen2, "remaining."
        break

raw_input("\nPress [Enter] for a new assault\n")
execfile("Risk.py")

My new problem is that in a real game of risk, an attacker and a defender will not always role 3 dice or 2 dice, sometimes they will roll a smaller number of dice. ex. an attacker may want to use 3 dice, but a defender only has 1 man left so he has to use 1 die. I need to know how to do that.

hehe i read about that little scenario when i looked up the rules of risk myself, seems like a complicated little game although i didn't read it that indepth. i'll mess about with mine, see what i can get working or not working.

p.s by the way that bit you do to sort the numbers from highest to lowest. python has a sort function for lists:

>>> alist = [1,2,3,4]
>>> alist.sort(reverse=True) # if you don't have the reverse, you will end up with it lowest to highest.
>>> print alist
[4,3,2,1]

I thank you VERY much for telling me about lists. I tinkered with them and have been able to pretty much make the same program run on alot less code.

import random

print "---Risk attack simulator---"
aArmy = input("Enter the size of the attacking army: ")
dArmy = input("Enter the size of the defending army: ")

loop = True
while loop:

    a1 = random.randint(1,6)
    a2 = random.randint(1,6)
    a3 = random.randint(1,6)
    d1 = random.randint(1,6)
    d2 = random.randint(1,6)
    
    alist = [a1,a2,a3]
    dlist = [d1,d2]
    
    alist.sort(reverse = True)
    dlist.sort(reverse = True)
    
    print "\nAttacker rolls:", alist[0], alist[1], alist[2]
    print "Defender rolls:", dlist[0], dlist[1]
    print "\nResults:" 
    print alist[0],alist[1], "vs.", dlist[0],dlist[1]
    
    roll1 = alist[0] - 1
    roll2 = alist[1] - 1
    
    aCount = 0
    dCount = 0

    if alist[0] > dlist[0]:
        dArmy -= 1
        dCount += 1
    if alist [1] > dlist[1]:
        dArmy -= 1
        dCount += 1
    if dlist[0] > roll1:
        aArmy -= 1
        aCount += 1
    if dlist[1] > roll2:
        aArmy -= 1
        aCount += 1

    print "\nAttacker looses:",aCount 
    print "Defender looses:",dCount
    print "\nAttacker now has:",aArmy
    print "Defender now has:",dArmy

    if dArmy < 1 or aArmy < 1:
        break

raw_input("")
execfile("RiskV2.py")

One quick question: How do I make the program refuse to take "0" or no answer for when it asks about the armies? If nothing is entered, then it errors and if 0 is entered... then that just doesn't make sense.

A more foolproof way would be to loop each data entry like this:

# let's say your attacking army can be from 1 tp 100

# keeps looping until entry is acceptable
aArmy = 0
while aArmy < 1 or aArmy > 100:
    try:
        aArmy = input("Enter the size of the attacking army (1 to 100): ")
    except NameError:  # a letter is entered
        aArmy = 0
print aArmy

glad you liked em, lists are wonderful things, lots of things can be done with them. Another great thing i love about lists is something called list comprehension.
List comprehension works in the following way:

[expression for item in sequence (if condition [I]not necesary[/I])] # Thats the rough outline
# Here's a simple example
>>> [1 for num in range(5)]
[1, 1, 1, 1, 1]

First i'm telling python to put '1' in the list, then i tell python it's going to keep putting '1' inside it untill the 'for num in range(5)' stops looping.

Here's an example that fits your code:

>>> alist = [random.randint(1,6) for num in range(3)]
>>> dlist = [random.randint(1,6) for num in range(2)]
>>> print alist
[4, 6, 1]
>>> print dlist
[3, 5]

In the example above, python understands that random.randint(1,6) is going to be placed in the list 3 times (3 times, because the for loop is looping 3 times; range(3))
list comprehension, simply 'compreses' loops eg if you wanted to do the above without using list comprehension, you would simply use a for loop:

>>> alist = []
>>> for num in range(3):
            alist.append(random.randint(1,6))
>>> print alist
[6, 3, 4]

if you need any more info on list comprehension, just ask.

happy coding

p.s in your code,

loop = True
while loop:
    ...

you don't need to store True in a variable, just do this:

while True:
    ...
# OR This
while 1:
    ...

alright I was able to finish the code, and now it changes the number of dice rolled according to the size of the armies. Again, like my orginal code, I'm sure there is way more code than needed for this. But it was the only way I could come up with a solution.

Code:

import random

dashes = "======================================"
clear = "\n"*100

print "---Risk attack simulator---\n"

aArmy = 0
while aArmy < 2:
    try:
        aArmy = input("Enter the size of the attacking army (2 or more): ")
    except NameError:
        aArmy = 0

dArmy = 0
while dArmy < 1:
    try:
        dArmy = input("Enter the size of the defending army (1 or more): ")
    except NameError:
        dArmy = 0

while True:

    if aArmy > 3:
        aNum = 3
    elif aArmy == 3:
        aNum = 2
    elif aArmy == 2:
        aNum = 1
    elif aArmy == 1:
        break

    if dArmy > 1:
        dNum = 2
    elif dArmy == 1:
        dNum = 1

    alist = [random.randint(1,6) for num in range(aNum)]
    dlist = [random.randint(1,6) for num in range(dNum)]
    
    alist.sort(reverse = True)
    dlist.sort(reverse = True)
    
    if aNum == 3 and dNum == 2:
        print dashes
        print "Attacker rolls:", alist[0], alist[1], alist[2]
        print "Defender rolls:", dlist[0], dlist[1]
        print "\nResults:" 
        print alist[0],alist[1], "vs.", dlist[0],dlist[1]
    
        roll1 = alist[0] - 1
        roll2 = alist[1] - 1
    
        aCount = 0
        dCount = 0

        if alist[0] > dlist[0]:
            dArmy -= 1
            dCount += 1
        if alist [1] > dlist[1]:
            dArmy -= 1
            dCount += 1
        if dlist[0] > roll1:
            aArmy -= 1
            aCount += 1
        if dlist[1] > roll2:
            aArmy -= 1
            aCount += 1

        print "\nAttacker looses:",aCount 
        print "Defender looses:",dCount
        print "\nAttacker now has:",aArmy
        print "Defender now has:",dArmy
        print dashes

    if aNum == 3 and dNum == 1:
        print dashes
        print "Attacker rolls:", alist[0], alist[1], alist[2]
        print "Defender rolls:", dlist[0]
        print "\nResults:" 
        print alist[0], "vs.", dlist[0]
    
        roll1 = alist[0] - 1
            
        aCount = 0
        dCount = 0

        if alist[0] > dlist[0]:
            dArmy -= 1
            dCount += 1
        if dlist[0] > roll1:
            aArmy -= 1
            aCount += 1
        
        print "\nAttacker looses:",aCount 
        print "Defender looses:",dCount
        print "\nAttacker now has:",aArmy
        print "Defender now has:",dArmy
        print dashes

    if aNum == 2 and dNum == 2:
        print dashes
        print "Attacker rolls:", alist[0], alist[1]
        print "Defender rolls:", dlist[0], dlist[1]
        print "\nResults:" 
        print alist[0],alist[1], "vs.", dlist[0],dlist[1]
    
        roll1 = alist[0] - 1
        roll2 = alist[1] - 1
    
        aCount = 0
        dCount = 0

        if alist[0] > dlist[0]:
            dArmy -= 1
            dCount += 1
        if alist [1] > dlist[1]:
            dArmy -= 1
            dCount += 1
        if dlist[0] > roll1:
            aArmy -= 1
            aCount += 1
        if dlist[1] > roll2:
            aArmy -= 1
            aCount += 1

        print "\nAttacker looses:",aCount 
        print "Defender looses:",dCount
        print "\nAttacker now has:",aArmy
        print "Defender now has:",dArmy
        print dashes

    if aNum == 2 and dNum == 1:
        print dashes
        print "Attacker rolls:", alist[0], alist[1]
        print "Defender rolls:", dlist[0]
        print "\nResults:" 
        print alist[0], "vs.", dlist[0]
    
        roll1 = alist[0] - 1
            
        aCount = 0
        dCount = 0

        if alist[0] > dlist[0]:
            dArmy -= 1
            dCount += 1
        if dlist[0] > roll1:
            aArmy -= 1
            aCount += 1
        
        print "\nAttacker looses:",aCount 
        print "Defender looses:",dCount
        print "\nAttacker now has:",aArmy
        print "Defender now has:",dArmy
        print dashes

    if aNum == 1 and dNum == 2:
        print dashes
        print "Attacker rolls:", alist[0]
        print "Defender rolls:", dlist[0], dlist[1]
        print "\nResults:" 
        print alist[0], "vs.", dlist[0]
    
        roll1 = alist[0] - 1
            
        aCount = 0
        dCount = 0

        if alist[0] > dlist[0]:
            dArmy -= 1
            dCount += 1
        if dlist[0] > roll1:
            aArmy -= 1
            aCount += 1
        
        print "\nAttacker looses:",aCount 
        print "Defender looses:",dCount
        print "\nAttacker now has:",aArmy
        print "Defender now has:",dArmy
        print dashes

    if aNum == 1 and dNum == 1:
        print dashes
        print "Attacker rolls:", alist[0]
        print "Defender rolls:", dlist[0]
        print "\nResults:" 
        print alist[0], "vs.", dlist[0]
    
        roll1 = alist[0] - 1
            
        aCount = 0
        dCount = 0

        if alist[0] > dlist[0]:
            dArmy -= 1
            dCount += 1
        if dlist[0] > roll1:
            aArmy -= 1
            aCount += 1
        
        print "\nAttacker looses:",aCount 
        print "Defender looses:",dCount
        print "\nAttacker now has:",aArmy
        print "Defender now has:",dArmy
        print dashes
        
    if dArmy < 1 or aArmy < 2 :
        break

if aArmy == 1:
    print "The defending army held off the assault with", dArmy, "left."
if dArmy == 0:
    print "The attacking army won with", aArmy, "left."

raw_input("\n==Press [enter] for a new assault==")
print clear
execfile("RiskV2.py")

Sweet man!, thats wicked. Nevermind the 'shortcuts' it works like a beaut'.
One thing though, when you enter large armies (eg: 10 v 10) it keeps running the simulation until the end, where it then shows you the final outcome, you could (if you wanted) put in a simple line:

raw_input("Hit Enter To Continue")

this would act as a 'pause' (very much like the one you have at the VERY end of your program) allowing the reader to read that particular battle then move on to the next when ready. All you would have to do is put it at the begining (or end) of the main 'while True' loop.

Great job though :)

it still errors if nothing is entered at all, how do i make it refuse to accept nothing for an answer.

where you have the 'try' and 'except' section of your code, your checking to see if there is a NameError, that means, if there is a NameError, it will catch it, however, it will not catch any others. If you don't specify a particular error to catch, then it will check for all errors.

in simple terms, just take NameError out of the except part, so your left with simply:

except:
   ...

however, if you want to return specific responses acording to specific errors you could have something along the lines of:

try:
    ...
except NameError:
    print "Please enter a number."
except SyntaxError:
    print "You must enter something"
except:
    print "Stop screwing about and just enter number!"

but that above isn't probably needed, as the user is only entering a number. so on except: should do it all.

Ok, I have a new problem now, i was planing on using the program i made using your guys' help here to make a graphical risk game, and i figured i would start simple with just a block of four countries.

from Tkinter import *

def addMen():
    addMenC = input("Where do you wish to add men? (country 1,2,3 or 4):")
    addMenLoop = True
    c1a = 0
    c2a = 0
    c3a = 0
    c4a = 0
    while addMenLoop:
        if addMenC == 1:
            c1a += 3
            country1Label.configure(text = str(c1a))
            break
        elif addMenC == 2:
            c2a += 3
            country2Label.configure(text = str(c2a))
            break
        elif addMenC == 3:
            c3a += 3
            country3Label.configure(text = str(c3a))
            break
        elif addMenC == 4:
            c4a += 3
            country1Label.configure(text = str(c4a))
            break
        
      

riskWindow = Tk()
riskWindow.title("Risk")    

country1TitleLabel = Label(riskWindow, text="Country 1", width=30)
country1TitleLabel.grid(row=0, column=0)

country2TitleLabel = Label(riskWindow, text="Country 2", width=30)
country2TitleLabel.grid(row=0, column=1)

country3TitleLabel = Label(riskWindow, text="Country 3", width=30)
country3TitleLabel.grid(row=2, column=0)

country4TitleLabel = Label(riskWindow, text="Country 4", width=30)
country4TitleLabel.grid(row=2, column=1)

country1Label = Label(riskWindow, text=str(c1a), width=30)
country1Label.grid(row=1, column=0)

country2Label = Label(riskWindow, text=str(c2a), width=30)
country2Label.grid(row=1, column=1)

country3Label = Label(riskWindow, text=str(c3a), width=30)
country3Label.grid(row=3, column=0)

country4Label = Label(riskWindow, text=str(c4a), width=30)
country4Label.grid(row=3, column=1)

armyButton = Button(riskWindow, text="Add men to a country", command=addMen)
armyButton.grid(row=4, column=0)

riskWindow.mainloop()

the problem i would like solved is that when you want to add men, it will make it add the 3 guys to the country, but if you want to add 3 more it does nothing. i need it to add the 3 guys, not just put 3 guys total on the country.

Once you go to GUI programming, your approach has to change past just making labels. User actions are initiated by clicking on buttons, data is entered in an entry widget, results and messages can be displayed in labels.

Get used to callback functions. Also, you might as well start a class Risk, so variables are transferred properly via the self. prefix. Just a little extra learning curve here! GUI programming is not too hard, just a mildly different way of thinking

can someone show me what ene uran is talking about, i dont quite get it. I understand doing everything through labels instead of the other window, but the way i program when im learning a language, is to do it as simple as possible from my point of view, even if its harder to use the program, then i slowly change the reast of the code to make it easier to use the program, i will do the same here.

Matt, as I look at your initial GUI code I can see two problems not related to Tkinter. I marked them with # !!!!!!!!!!!!!!!!, those will be errors!

from Tkinter import *

def addMen():
    addMenC = input("Where do you wish to add men? (country 1,2,3 or 4):")
    addMenLoop = True
    c1a = 0
    c2a = 0
    c3a = 0
    c4a = 0
    while addMenLoop:
        if addMenC == 1:
            c1a += 3
            # !!!!!!!!!!!!!!!!!
            # country1Label was defined outside the function
            # and is not known inside this function
            # dito for the three other labels
            country1Label.configure(text = str(c1a))
            break
        elif addMenC == 2:
            c2a += 3
            country2Label.configure(text = str(c2a))
            break
        elif addMenC == 3:
            c3a += 3
            country3Label.configure(text = str(c3a))
            break
        elif addMenC == 4:
            c4a += 3
            country1Label.configure(text = str(c4a))
            break
        
      

riskWindow = Tk()
riskWindow.title("Risk")

country1TitleLabel = Label(riskWindow, text="Country 1", width=30)
country1TitleLabel.grid(row=0, column=0)

country2TitleLabel = Label(riskWindow, text="Country 2", width=30)
country2TitleLabel.grid(row=0, column=1)

country3TitleLabel = Label(riskWindow, text="Country 3", width=30)
country3TitleLabel.grid(row=2, column=0)

country4TitleLabel = Label(riskWindow, text="Country 4", width=30)
country4TitleLabel.grid(row=2, column=1)

# !!!!!!!!!!!!!!!!!!!!!!!!!
# c1a was defined in the function, but is not known outside the function
# dito for c2a, c3a, c4a in the lines that follow
country1Label = Label(riskWindow, text=str(c1a), width=30)
country1Label.grid(row=1, column=0)

country2Label = Label(riskWindow, text=str(c2a), width=30)
country2Label.grid(row=1, column=1)

country3Label = Label(riskWindow, text=str(c3a), width=30)
country3Label.grid(row=3, column=0)

country4Label = Label(riskWindow, text=str(c4a), width=30)
country4Label.grid(row=3, column=1)

armyButton = Button(riskWindow, text="Add men to a country", command=addMen)
armyButton.grid(row=4, column=0)

riskWindow.mainloop()

Now, you could make all those variables global for the function.

from Tkinter import *

def addMen():
    # make these labels global so they can be used in the function
    global country1Label
    global country2Label
    global country3Label
    global country4Label
    # make these variables global, so changes transfer out
    global c1a, c2a, c3a, c4a
    addMenC = input("Where do you wish to add men? (country 1,2,3 or 4):")
    addMenLoop = True
    while addMenLoop:
        if addMenC == 1:
            c1a += 3
            country1Label.configure(text = str(c1a))
            break
        elif addMenC == 2:
            c2a += 3
            country2Label.configure(text = str(c2a))
            break
        elif addMenC == 3:
            c3a += 3
            country3Label.configure(text = str(c3a))
            break
        elif addMenC == 4:
            c4a += 3
            country1Label.configure(text = str(c4a))
            break


riskWindow = Tk()
riskWindow.title("Risk")

c1a = 0
c2a = 0
c3a = 0
c4a = 0

country1TitleLabel = Label(riskWindow, text="Country 1", width=30)
country1TitleLabel.grid(row=0, column=0)

country2TitleLabel = Label(riskWindow, text="Country 2", width=30)
country2TitleLabel.grid(row=0, column=1)

country3TitleLabel = Label(riskWindow, text="Country 3", width=30)
country3TitleLabel.grid(row=2, column=0)

country4TitleLabel = Label(riskWindow, text="Country 4", width=30)
country4TitleLabel.grid(row=2, column=1)

country1Label = Label(riskWindow, text=str(c1a), width=30)
country1Label.grid(row=1, column=0)

country2Label = Label(riskWindow, text=str(c2a), width=30)
country2Label.grid(row=1, column=1)

country3Label = Label(riskWindow, text=str(c3a), width=30)
country3Label.grid(row=3, column=0)

country4Label = Label(riskWindow, text=str(c4a), width=30)
country4Label.grid(row=3, column=1)

armyButton = Button(riskWindow, text="Add men to a country", command=addMen)
armyButton.grid(row=4, column=0)

riskWindow.mainloop()

The next issue will be that input() happens in the command window, and you have to switch back and forth between the tk window and the command winow. You will get tired of that real quick, that's where the entry widget comes to the rescue. Now your input happens in the tk window. By the way, Tkinter also has a spinbox widget and you can make the value jump by three on every click of the up arrow.

Anyway, happy explorations! Best way to learn!

My bad! Actually, as I was playing around with the code, I discovered that the following global defines were not needed:

# make these labels global so they can be used in the function 
    global country1Label 
    global country2Label 
    global country3Label 
    global country4Label
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.