例外和输出

时间:2016-05-16 13:41:18

标签: python if-statement exception

调用我的函数时遇到问题,我收到了错误的输出,我将在后面的文章中解释。

这些是我正在使用的资源:

Main_Building=[10,12,14,17,21,25,30,36,43,52,62,74,89,107,128,154,185,222,266,319,383,460,552,662,795,954,1145,1374,1648,1978]
Barracks=[16,19,23,28,33,40,48,57,69,83,99,119,143,171,205,247,296,355,426,511,613,736,883,1060,1272]
Stables=[20,24,29,35,41,50,60,72,86,103,124,149,178,214,257,308,370,444,532,639]
Workshop=[24,29,35,41,50,60,72,86,103,124,149,178,214,257,308]
Blacksmith=[19,23,27,33,39,47,57,68,82,98,118,141,169,203,244,293,351,422,506,607]
Market=[10,12,14,17,21,25,30,36,43,52,62,74,89,107,128,154,185,222,266,319,383,460,552,662,795]
Axe=[6,7,9,10,12,15,18,21,26,31,37,45,53,64,77,92,111,133,160,192,230,276,331,397,477,572,687,824,989,1187]
Clay_Pit=[6,7,9,10,12,15,18,21,26,31,37,45,53,64,77,92,111,133,160,192,230,276,331,397,477,572,687,824,989,1187]
Mine=[6,7,9,10,12,15,18,21,26,31,37,45,53,64,77,92,111,133,160,192,230,276,331,397,477,572,687,824,989,1187]
Settler_House=[5,6,7,9,10,12,15,18,21,26,31,37,45,53,64,77,92,111,133,160,192,230,276,331,397,477,572,687,824,989]
Warehouse=[6,7,9,10,12,15,18,21,26,31,37,45,53,64,77,92,111,133,160,192,230,276,331,397,477,572,687,824,989,1187]
Wall=[8,10,12,14,17,20,24,29,34,41,50,59,71,86,103,123,148,177,213,256]

这是我的代码:

def buildings(points):
    for i in range(0,30):
        try:
            if Main_Building[i]>=points:
                del Main_Building[i:]
            if Barracks[i]>=points:
                del Barracks[i:]
            if Stables[i]>=points:
                del Stables[i:]
            if Workshop[i]>=points:
                del Workshop[i:]
            if Blacksmith[i]>=points:
                del Blacksmith[i:]
            if Market[i]>=points:
                del Market[i:]
            if Axe[i]>=points:
                del Axe[i:]
            if Clay_Pit[i]>=points:
                del Clay_Pit[i:]
            if Mine[i]>=points:
                del Mine[i:]
            if Settler_House[i]>=points:
                del Settler_House[i:]
            if Warehouse[i]>=points:
                del Warehouse[i:]
            if Wall[i]>=points:
                del Wall[i:]                  
        except IndexError:
            continue

问题在于,当涉及到铁匠的状态时,它看起来只是条件才通过,而其他人继续在华尔条件下也是如此。条件是确定停止的位置并删除列表的其余部分以供进一步使用。列表是不同的长度所以我使用简单的例外,当它超出范围时,它只是跳过并继续下一个条件。

def建筑物(100)时的建议输出:

Blacksmith=[19,23,27,33,39,47,57,68,82,98]

实际输出是完整列表,没有任何变化。这同样适用于持续的条件。

我尝试了什么:

  1. 我试图重启Python,但遗憾的是不是。

  2. 如果我拼错了变量名。

  3. 在每种条件下重做间距。

  4. 也许解决方案但没有效果,添加到每个条件尝试异常?(在我看来不是一个好主意)。

    为什么会跳过这些条件?

    感谢您的帮助和时间。

2 个答案:

答案 0 :(得分:0)

如果try-except之间发生任何错误,Python会传递或继续try-except之间的所有代码。

try:
    a = int(input("integer: "))
    print("print this")
    print("print that")
except:
    pass

输出:

>>> 
integer: ab
>>>

看到print thisprint that未打印。你应该逐个发现错误。

try:
    a = int(input("integer: "))
except:
    pass
print("print this")
print("print that")

>>> 
integer: ab
print this
print that
>>> 

答案 1 :(得分:0)

Continue将返回当前循环的开头并获取下一个索引。这意味着如果中途发生错误,则跳过后半部分。这也很冗长。这是我的解决方案。

data = [Main_Building, Barracks, Stables, Workshop, Blacksmith, Market, Axe, Clay_Pit, Mine, Settler_House, Warehouse, Wall]
def buildings(points):
    for building in data: # loop through each building seperately, shortens code and removes arbitrary loop number
        for point in building: # loop through each buildings index, same as your code
            if point >= points:
                del building[building.index(point):]
buildings(20)
print data