---
title: 16.實作5:樂透中獎機率程式
tags: Python 進階語法與應用, 筆記
---
## Practice
import random
lottery = set()
times=0
while len(lottery)<7:
n = random.randint(1,48)
lottery.add(n)
times=times+1
print(lottery)
print(times)
---
import random
prize = set()
while len(prize)<7:
n = random.randint(1,48)
prize.add(n)
print("頭獎彩券:", prize)
#不固定次數 while
#當難寫實 先一直做 空檔再停止
#無窮迴圈 手動停止(使用時機:當你while條件寫不出來時)
times=0
while True:
lottery = set()
while len(lottery)<7:
n = random.randint(1,48)
lottery.add(n)
print("我買的彩券:", lottery)
times = times+1
total = 0
for n in lottery:
if n in prize:
print(n, "中了")
total = total+1
print("中了",total, "個數字")
if total>=4:
break
print("總共買了",times,"張才中")
----
# END