# 請問怎麼用Python 3.X系列寫統一發票中獎程式?

1. 可輸入組數無限

2. 輸入格式非八位數字則顯示

"輸入統一編號格式錯誤, 請重新輸入."。

3. 需能自動判定對應之獎項與金額。

### 1 個解答

• 1 個月前

#!/usr/bin/env python3

import urllib.request

import re

fp = urllib.request.urlopen("http://invoice.etax.nat.gov.tw")

myStr = myBytes.decode("utf8")

digitsLen = 8

digitsGrp = 5

grp0 = r'([\d]{})'.format("{"+str(digitsLen)+'}')

grp1 = r'>([\d]{3})<'

winners = re.findall(grp0, myStr)

winner3 = re.findall(grp1, myStr)

winner5 = winners[0:digitsGrp]

winner5.append("00000" + winner3[0])

prizes = ( 10000000, 2000000, 200000, 40000, 10000, 4000, 1000, 200, 200)

titles = ( '特別獎', '特獎', '頭獎', '二獎', '三獎', '四獎', '五獎', '六獎', '增開六獎')

shifts = (((0,1),0),((1,2),0),((2,5),0),((2,5),1),((2,5),2),((2,5),3),((2,5),4),((2,5),5), ((5,6),5))

while True:

inNumber = input('Give me your number ({} digits, 0 to stop): '.format(digitsLen))

if 1 == len(inNumber) and '0' == inNumber:

print('Bye!')

exit(0)

if digitsLen != len(inNumber):

else:

found = False

for i in range(len(shifts)):

code = shifts[i]

for j in range(int(code[0][0]), int(code[0][1])):

wincode = winner5[j]

shft = int(code[1])

if wincode[shft:] == inNumber[shft:]:

print('You have won {} ({}) for NT\${:,}.'.format(titles[i], wincode[shft:], prizes[i]))

found = True

break

if found: break