【问题标题】:Exceptions and Output异常和输出
【发布时间】:2016-09-12 07:53:45
【问题描述】:

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

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

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. 在每个条件下重做间距。

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

为什么它会跳过条件?

感谢您的帮助和时间。

【问题讨论】:

    标签: python if-statement exception


    【解决方案1】:

    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
    

    【讨论】:

    • 谢谢,我明白了。我犯了多么糟糕的错误。
    【解决方案2】:

    如果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
    >>> 
    

    【讨论】:

      猜你喜欢
      • 2012-02-12
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      相关资源
      最近更新 更多