在列表中添加数字但仅在特定数字之后添加

时间:2014-11-21 15:57:11

标签: python

我正在写一个保龄球得分的脚本。

规则

- 如果你得到一个STRIKE,你得到10分加上你得分的接下来的两个球。

我的问题:有没有办法在10出现后在列表中添加下两个数字。到目前为止,我的代码只是将用户评分并将其存储在列表中。

def f54():
    total_pins= 10
    max_score= 300
    frames= 20
    count= 1
    score=0
    zval = []
    yval = []

    for i in range(1,11):

        2_ahaed = i+2
        1_ahead = i+1

        z=int(input('What was your score for ball 1 frame ' +str(i)+':'))
        if z != 10:
            y=int(input('What was your score for ball 2 frame ' +str(i)+':'))

        if z > total_pins:
            print('this number is too large, re enter all scores')
            f54()
        if z < 10:
            zval.append(z)
        if z == 10:
            print('Strike!')


        if y > total_pins:
            print('this number is too large, re enter all scores')
            f54()
        if y < 10:
            yval.append(y)
        if y == 10-z:
            print('spare!')

2 个答案:

答案 0 :(得分:1)

我对保龄球得分规则不太熟悉,但这应该是你要求的:

scores = [1,2,10,3,4,5,10,1]

ret = [0 for x in (scores)]

for i in xrange(len(scores)):
        ret[i] = sum(scores[i:i+3]) if scores[i] == 10 else scores[i] 

print "Final score", sum(ret)

答案 1 :(得分:0)

是的你可以。 。 List [index:]表示给定索引号After的所有值。 。 List [:index]表示给定索引号Before的所有值。

在这种情况下,

zval = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for val in zval:
   if val==7:
     `print zval[val:]`
      print sum(zval[val:])