Python程序停止循环并随机卡住

时间:2015-03-05 08:45:02

标签: python python-2.7

我一直在努力学习Python已有一段时间了,我一直在努力让这个简单的程序工作。

正如你所看到的,我想要做的是获得4个“战斗”值,直到剩下一个。从第11循环到第22循环,它一直很好,很花哨,然后就停止了。我是一个完全的初学者,不知道我做错了什么。

import random
import math
import os

print '     ----------------------------------'
print '       UNIVERSAL ALL-STARS DEATHMATCH'
print '     ----------------------------------'
print ''
print 'CHOOSE FOUR CHARACTERS TO FIGHT IN AN FFA DEATHMATCH!'
print ''

#User input for character names.
characterInputted = False
while characterInputted == False:
    character1 = raw_input('Input Character 1 Name:')
    character2 = raw_input('Input Character 2 Name:')
    character3 = raw_input('Input Character 3 Name:')
    character4 = raw_input('Input Character 4 Name:')
    if character1 == character2:
        print 'Cannot have any characters with identical names!'
        characterInputted = False
    elif character1 == character3:
        print 'Cannot have any characters with identical names!'
        characterInputted = False
    elif character1 == character4:
        print 'Cannot have any characters with identical names!'
        characterInputted = False
    elif character2 == character3:
        print 'Cannot have any characters with identical names!'
        characterInputted = False
    elif character2 == character4:
        print 'Cannot have any characters with identical names!'
        characterInputted = False
    elif character3 == character4:
        print 'Cannot have any characters with identical names!'
        characterInputted = False
    else:
        characterInputted = True

def combat_roll():
    combatValid = False
    while combatValid == False:
        rollAtk = random.randint(1, 4)
        rollDef = random.randint(1, 4)
        if rollAtk == rollDef:
            combatValid = False
#Roll for char1
        elif rollAtk == character1_Number:
            if character1_Dead == True:
                combatValid = False
            elif rollDef == character2_Number:
                if character2_Dead == True:
                    combatValid = False
                else:
                    rollAttacker = character1
                    rollDefender = character2
                    combatValid = True
                    return rollAttacker, rollDefender
            elif rollDef == character3_Number:
                if character3_Dead == True:
                    combatValid = False
                else:
                    rollAttacker = character1
                    rollDefender = character3
                    combatValid = True
                    return rollAttacker, rollDefender
            elif rollDef == character4_Number:
                if character4_Dead == True:
                    combatValid = False
                else:
                    rollAttacker = character1
                    rollDefender = character4
                    combatValid = True
                    return rollAttacker, rollDefender
#Roll for char2
        elif rollAtk == character2_Number:
            if character2_Dead == True:
                combatValid = False
            elif rollDef == character1_Number:
                if character1_Dead == True:
                    combatValid = False
                else:
                    rollAttacker = character2
                    rollDefender = character1
                    combatValid = True
                    return rollAttacker, rollDefender
            elif rollDef == character3_Number:
                if character3_Dead == True:
                    combatValid = False
                else:
                    rollAttacker = character2
                    rollDefender = character3
                    combatValid = True
                    return rollAttacker, rollDefender
            elif rollDef == character4_Number:
                if character4_Dead == True:
                    combatValid = False
                else:
                    rollAttacker = character2
                    rollDefender = character4
                    combatValid = True
                    return rollAttacker, rollDefender
#Roll for char3
        elif rollAtk == character3_Number:
            if character1_Dead == True:
                combatValid = False
            elif rollDef == character1_Number:
                if character2_Dead == True:
                    combatValid = False
                else:
                    rollAttacker = character3
                    rollDefender = character1
                    combatValid = True
                    return rollAttacker, rollDefender
            elif rollDef == character2_Number:
                if character2_Dead == True:
                    combatValid = False
                else:
                    rollAttacker = character3
                    rollDefender = character2
                    combatValid = True
                    return rollAttacker, rollDefender
            elif rollDef == character4_Number:
                if character4_Dead == True:
                    combatValid = False
                else:
                    rollAttacker = character3
                    rollDefender = character4
                    combatValid = True
                    return rollAttacker, rollDefender
#Roll for char4
        elif rollAtk == character4_Number:
            if character1_Dead == True:
                combatValid = False
            elif rollDef == character1_Number:
                if character1_Dead == True:
                    combatValid = False
                else:
                    rollAttacker = character4
                    rollDefender = character1
                    combatValid = True
                    return rollAttacker, rollDefender
            elif rollDef == character2_Number:
                if character2_Dead == True:
                    combatValid = False
                else:
                    rollAttacker = character4
                    rollDefender = character2
                    combatValid = True
                    return rollAttacker, rollDefender
            elif rollDef == character3_Number:
                if character3_Dead == True:
                    combatValid = False
                else:
                    rollAttacker = character4
                    rollDefender = character3
                    combatValid = True
                    return rollAttacker, rollDefender
        else:
            combatValid = False

#Roundtick set to zero
roundNumber = 0

#Characters randomly generated stats.
character1_Attackbonus = random.randint(0, 2)
character1_Number = 1
character1_Dead = False
character1_LifeCount = 1

character2_Attackbonus = random.randint(0, 2)
character2_Number = 2
character2_Dead = False
character2_LifeCount = 1

character3_Attackbonus = random.randint(0, 2)
character3_Number = 3
character3_Dead = False
character3_LifeCount = 1

character4_Attackbonus = random.randint(0, 2)
character4_Number = 4
character4_Dead = False
character4_LifeCount = 1

gameover = False
while gameover == False:
    os.system('cls')
    lifeCount_Total = [character1_LifeCount +
                       character2_LifeCount + character3_LifeCount +
                       character4_LifeCount]
    roundNumber = roundNumber + 1
    print 'Round #%d!' % roundNumber
    raw_input('Press ENTER to continue.')
    attackChance = random.randint(0, 10)
    if lifeCount_Total > 2 == True:
        gameover = True
    elif attackChance < 3:
        print "No one attacked this round!"
    elif attackChance > 3:
        Attacker, Defender = combat_roll()
        print Attacker + ' attacked ' + Defender + '.'
        Attackbase = random.randint(0, 10)
#Combat calculations!
        combatCalc = False
        while combatCalc == False:
            if Attacker == character1:
                AttackTotal = Attackbase + character1_Attackbonus
                if Defender == character2:
                    if AttackTotal > 5:
                        character2_Dead = True
                        character2_LifeCount = 0
                        print '%s killed %s!' % (Attacker, Defender)
                        combatCalc = True
                    else:
                        print '%s failed to kill %s!' % (Attacker, Defender)
                        combatCalc = True
                elif Defender == character3:
                    if AttackTotal > 5:
                        character3_Dead = True
                        character3_LifeCount = 0
                        print '%s killed %s!' % (Attacker, Defender)
                        combatCalc = True
                    else:
                        print '%s failed to kill %s!' % (Attacker, Defender)
                    combatCalc = True
                elif Defender == character4:
                    if AttackTotal > 5:
                        character4_Dead = True
                        character4_LifeCount = 0
                        print '%s killed %s!' % (Attacker, Defender)
                        combatCalc = True
                    else:
                        print '%s failed to kill %s!' % (Attacker, Defender)
                        combatCalc = True

            elif Attacker == character2:
                AttackTotal = Attackbase + character2_Attackbonus
                if Defender == character1:
                    if AttackTotal > 5:
                        character1_Dead = True
                        character1_LifeCount = 0
                        print '%s killed %s!' % (Attacker, Defender)
                        combatCalc = True
                    else:
                        print '%s failed to kill %s!' % (Attacker, Defender)
                        combatCalc = True
                elif Defender == character3:
                    if AttackTotal > 5:
                        character3_Dead = True
                        character3_LifeCount = 0
                        print '%s killed %s!' % (Attacker, Defender)
                        combatCalc = True
                    else:
                        print '%s failed to kill %s!' % (Attacker, Defender)
                    combatCalc = True
                elif Defender == character4:
                    if AttackTotal > 5:
                        character4_Dead = True
                        character4_LifeCount = 0
                        print '%s killed %s!' % (Attacker, Defender)
                        combatCalc = True
                    else:
                        print '%s failed to kill %s!' % (Attacker, Defender)
                        combatCalc = True

            elif Attacker == character3:
                AttackTotal = Attackbase + character3_Attackbonus
                if Defender == character1:
                    if AttackTotal > 5:
                        character1_Dead = True
                        character1_LifeCount = 0
                        print '%s killed %s!' % (Attacker, Defender)
                        combatCalc = True
                    else:
                        print '%s failed to kill %s!' % (Attacker, Defender)
                        combatCalc = True
                elif Defender == character2:
                    if AttackTotal > 5:
                        character2_Dead = True
                        character2_LifeCount = 0
                        print '%s killed %s!' % (Attacker, Defender)
                        combatCalc = True
                    else:
                        print '%s failed to kill %s!' % (Attacker, Defender)
                    combatCalc = True
                elif Defender == character4:
                    if AttackTotal > 5:
                        character4_Dead = True
                        character4_LifeCount = 0
                        print '%s killed %s!' % (Attacker, Defender)
                        combatCalc = True
                    else:
                        print '%s failed to kill %s!' % (Attacker, Defender)
                        combatCalc = True

            elif Attacker == character4:
                AttackTotal = Attackbase + character4_Attackbonus
                if Defender == character1:
                    if AttackTotal > 5:
                        character1_Dead = True
                        character1_LifeCount = 0
                        print '%s killed %s!' % (Attacker, Defender)
                        combatCalc = True
                    else:
                        print '%s failed to kill %s!' % (Attacker, Defender)
                        combatCalc = True
                elif Defender == character2:
                    if AttackTotal > 5:
                        character2_Dead = True
                        character2_LifeCount = 0
                        print '%s killed %s!' % (Attacker, Defender)
                        combatCalc = True
                    else:
                        print '%s failed to kill %s!' % (Attacker, Defender)
                    combatCalc = True
                elif Defender == character3:
                    if AttackTotal > 5:
                        character3_Dead = True
                        character3_LifeCount = 0
                        print '%s killed %s!' % (Attacker, Defender)
                        combatCalc = True
                    else:
                        print '%s failed to kill %s!' % (Attacker, Defender)
                        combatCalc = True
            else:
                combatCalc = False        
    else:
        gameover = False

print character1
print character2
print character3
print character4

1 个答案:

答案 0 :(得分:-1)

我测试了你的代码,似乎它没有停止但是进入函数combat_roll()并且卡在其中。如果我打印combatValid,我会获得:

`false 
 false
 false
 ...