本帖最后由 小锟 于 2017-9-1 12:16 编辑 import re
def count(str1):
scores = 0
every = str1.split(' ')
try:
for i in range(10):
scores += guize(every[i:] , scores)
return scores
except TypeError:
return 'N/A'
def guize(every,scores):
every = ''.join(every)
#every2 = every[:2] + re.sub(r'(\d)/', r'\1X', every[2:])
every3 = re.sub(r'\d/', '0X', every[:3])
try:
s = [ (int(i) if i != 'X' else 10) for i in list(every3) ]
#print(sum(s[:3]) if 'X' in every[0] or '/' in every[:2] else sum(s[:2]))
return sum(s) if 'X' in every[0] or '/' in every[:2] else sum(s[:2])
except ValueError :
return 'N/A'
bowling_0 = 'X X X X X X X X X XXX' #300
bowling_1 = '9/ 9/ 9/ 9/ 9/ 9/ 9/ 9/ 9/ 9/9' #190
bowling_2 = '90 9/ X X X 9/ X 8/ X 9/X' #208
bowling_3 = '9/ 8/ X 81 9/ X X 9/ 8/ X81' #192
bowling_4 = '9/ 8/ X 9/ X X X 9/ 8/ X9/' #215
bowling_5 = '9/ 0/ X 09 X X 8/ 09 8/ 0/0' #145
for i in [bowling_0 ,bowling_1,bowling_2,bowling_3,bowling_4,bowling_5]:
print(count(i))
最后发现只用考虑前三个就行了 |