如何修复while循环?

时间:2014-06-08 06:06:45

标签: python

所以我为练习编写游戏风险,但我遇到了问题,所以我在这里。 基本上,在

期间
while numAttacking>0:
    print("numAttacking=",numAttacking)
    if defensiveForce>0:
        print("defensiveForce=",defensiveForce)
        for x in range(numAttacking):
            defenseRate=random.randint(1,12)
            attackRate=random.randint(1,12)
            if defenseRate>=attackRate:
                REGION_DATA[attacker]['TROOP_COUNT']=REGION_DATA[attacker]['TROOP_COUNT']-1
                print("Lost one unit")
            else:
                REGION_DATA[defender]['TROOP_COUNT']=REGION_DATA[defender]['TROOP_COUNT']-1
                print("Killed one unit")

我的代码停顿并陷入while循环。我希望很清楚我想要什么,但简单地说我希望它能够运行,以便用户输入他们正在攻击的部队数量,类似于RISK在现实生活中的工作方式,然后代码循环直到攻击者或防御者用完为止分别用于攻击或防御的单位。

import random

regions=    ['ALASKA','NORTHWEST_TERRITORY','GREENLAND','ALBERTA','ONTARIO','EASTERN_CANADA','WESTERN_USA','EASTERN_USA','CENTRAL_AMERICA']

REGION_DATA={'ALASKA'                :{'OWNER':'Player 1',   'TROOP_COUNT':3,   'Bordering':['NORTHWEST_TERRITORY','ALBERTA']},
         'NORTHWEST_TERRITORY'   :{'OWNER':'Player 1',   'TROOP_COUNT':4,   'Bordering':['ALASKA','ALBERTA','ONTARIO','GREENLAND']},
         'GREENLAND'             :{'OWNER':'Player 1',   'TROOP_COUNT':4,   'Bordering':['NORTHWEST_TERRITORY','ONTARIO','EASTERN_CANADA']},
         'ALBERTA'               :{'OWNER':'Player 1',   'TROOP_COUNT':7,   'Bordering':['ALASKA','NORTHWEST_TERRITORY','ONTARIO','WESTERN_USA']},
         'ONTARIO'               :{'OWNER':'Player 1',   'TROOP_COUNT':7,   'Bordering':['NORTHWEST_TERRITORY','GREENLAND','EASTERN_CANADA','EASTERN_USA','WESTERN_USA','ALBERTA']},
         'EASTERN_CANADA'        :{'OWNER':'Player 2',   'TROOP_COUNT':6,   'Bordering':['GREENLAND','ONTARIO','EASTERN_USA']},
         'WESTERN_USA'           :{'OWNER':'Player 2',   'TROOP_COUNT':7,   'Bordering':['ALBERTA','ONTARIO','EASTERN_USA','CENTRAL_AMERICA']},
         'EASTERN_USA'           :{'OWNER':'Player 2',   'TROOP_COUNT':7,   'Bordering':['ONTARIO','WESTERN_USA','EASTERN_CANADA','CENTRAL_AMERICA']},
         'CENTRAL_AMERICA'       :{'OWNER':'Player 2',   'TROOP_COUNT':5,   'Bordering':['WESTERN_USA','EASTERN_USA']}
        }
def invade(attacker,defender):
#print("16")
attacker=attacker.upper().replace(" ","_")
defender=defender.upper().replace(" ","_")
#print(attacker,defender)
defensiveForce=REGION_DATA[defender]['TROOP_COUNT']
attackForce=REGION_DATA[attacker]['TROOP_COUNT']
#print(attackForce,defensiveForce)
#print("22")
print("You have ",attackForce," troops available to attack with.")
print("How many troops are you attacking with?")
numAttacking=int(raw_input())
if numAttacking>attackForce:
    print "You don't have that many troops available!"+'\n'+"You only have",attackForce," troops available."+'\n'+"How many troops will you use?"
    numAttacking=raw_input()
while numAttacking>0:
    print("numAttacking=",numAttacking)
    if defensiveForce>0:
        print("defensiveForce=",defensiveForce)
        for x in range(numAttacking):
            defenseRate=random.randint(1,12)
            attackRate=random.randint(1,12)
            if defenseRate>=attackRate:
                REGION_DATA[attacker]['TROOP_COUNT']=REGION_DATA[attacker]['TROOP_COUNT']-1
                print("Lost one unit")
            else:
                REGION_DATA[defender]['TROOP_COUNT']=REGION_DATA[defender]['TROOP_COUNT']-1
                print("Killed one unit")
    else:
        REGION_DATA[defender]['OWNER']=REGION_DATA[attacker]['OWNER']
        print("45")
        break  

print(REGION_DATA[attacker]['TROOP_COUNT'])

我的问题是

invade('ontario','eastern usa') 

输出

('You have ', 7, ' troops available to attack with.')
How many troops are you attacking with?

(我稍后会修复括号和引号,我确实知道导致这种情况的原因,但我现在太懒了。)

我回复

3

然后代码卡在输出一堆打印命令但从不退出while循环。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在代码顶部,您已设置defensiveCount。但是,您不会在while循环内更​​新该数量。它永远不会到达你的if / else审判的“其他”部分,因此它将继续试图继​​续攻击和杀死部队,甚至进入负面。 numAttacking也不会改变,所以它不会自动中断while循环。

编辑:

您可能认为正在发生的是您将defensiveCount设置为列表中的金额。但是,这只是在defensiveCount的定义中将其设置为该数字,除非明确告知,否则该数字不会改变。