异常和输出

2024-05-19 19:28:48 发布

您现在位置:Python中文网/ 问答频道 /正文

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

以下是我正在使用的资源:

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

问题是,当谈到铁匠的条件,它看起来在我看来,条件只是通过,和其他继续墙条件相同。条件是确定在何处停止并删除列表的其余部分以供进一步使用。列表的长度不同,所以我使用了简单的exception,当它超出范围时,它只是跳过并继续下一个条件

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

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

实际输出是整个列表,没有任何变化。这同样适用于持续状态

我尝试的是:

  1. 我试图重新启动Python,但不幸的是,没有成功

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

  3. 每种情况下都要重做间隔

也许是解决方法,但不是有效的,加上每个条件尝试例外?(在我看来不是一个好主意)

为什么它跳过了条件

谢谢你的帮助和时间


Tags: ifmain条件marketpointsworkshopdelbuilding
2条回答

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

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

相关问题 更多 >