错误:列表索引超出for循环范围

时间:2018-06-18 05:49:19

标签: python python-3.x

所以我制作了一个计算和打印得分的数量,打击的碗,投球手和击球手在板球比赛中罢工的程序,但是当我完成我的代码时:

name = input('enter the name of the bowler\n=')  # we ask for the input
    bat_strike = input('enter the name of the batter on strike\n=')
    bat_non_strike = input('enter the name of the batter on the non strike\n=')
    bowls = 6  # this is the max number of bowls in a over
    bowls_bowled = 0
    runs_list = []
    bowls_list = []
    batter_list = []
    batter_list.append(bat_strike)
    for j in range(0, bowls):
        bowl_type = int(input('enter the number corresponding to the option regarding the bowl bowled right now\n1)wide bowl\n2)simple bowl\n3)no ball\n4)dead bowl\n5)1D\n= '))
        if bowl_type == 1:
            bowls_list.append('wd')
            runs_list.append(1)
            batter_list.append(bat_strike)
            bowls += 1
        elif bowl_type == 2:
            run = int(input('enter the number of runs scored on that bowl 1 for 1 run 2 for 2 runs 3 for 3 runs 4 for 4 runs or 0 for a dot ball\n='))
            if run == 1 or run == 3:
                batter_on_strike = input('enter wether the batter on strike is on non strike? y or n\n=')
                if batter_on_strike == 'y':
                    c = bat_strike
                    bat_non_strike = bat_strike
                    bat_non_strike = c
                    batter_list.append(bat_strike)
            runs_list.append(run)
            bowls_list.append(1)
            bowls_bowled += 1
        elif bowl_type == 3:
            bowls_list.append('no ball')
            runs_list.append(1)
            bowls += 1
            batter_list.append(bat_strike)
        elif bowl_type == 4:
            bowls_list.append('dead bowl')
            bowls += 1
            batter_list.append(bat_strike)
        elif bowl_type == 5:
            bowls_list.append('1D')
            bowls += 1
            runs_list.append(1)
            bowls_bowled += 1
            batter_list.append(bat_strike)

    for i in range(0, bowls-1):
        print(f'bowler={name} batter on strike={batter_list[i]} bowl bowled={bowls_list[i]} runs scored={runs_list[i]}')

它会产生以下错误

  

print(f'bowler = {name} batter on strike = {batter_list [i]} bow bowled = {bowls_list [i]} run scored = {runs_list [i]}')   IndexError:列表索引超出范围

我想用每个碗的for循环显示结果,但我一直收到这个错误 任何人都有任何想法如何解决问题或更好的代码?

1 个答案:

答案 0 :(得分:0)

如果bowl_type为5,或者它是2,并且击球或非击球击球的击球手不是y,则不要追加任何内容batter_list。因此,如果发生这种情况,batter_list将不会与您的其他列表一样长。

如果所有6个碗都是5型,那么batter_list将为空,因此当然batter_list[4]会出错。

在这种情况下,您不清楚想要会发生什么,所以你应该如何修复它并不明显。

在这种情况下,一种可能性是batter_list.append(None)。或者也许是batter_list.append("n/a"),或者......您希望稍后在print中显示的内容。

另一种可能性是用一个dicts列表替换所有这些单独的列表:

bowl_info_list = []
for j in range(0, bowls):
    # ...
    if bowl_type == 1:
        bowl_info_list.append({'bowl': 'wd', 'runs': 1, 'batter': bat_strike})
# ...

for bowl_info in bowl_info_list:
    print(f'bowler={name} batter on strike={bowl_info["batter"]} bowl bowled={bowl_info["bowl"]} runs scored={bowl_info["runs"]}')

或者,甚至可能更好,使用class(可能是namedtuple@dataclass)而非dict。