---
title: 17.群集-tuple說明與實作
tags: Python 進階語法與應用, 筆記
---
## 群集
## 1. list清單 : 同類型 隊伍
- 1.[]
- 2.[座號]
## 2. dictionary 字典:聚集不同類型
- 1.{:}
- 2.["名字"]
## 3. set:“不重複”, “同類型” (不重複的list)
- 1.{} 不用:
- 2.不重複
- 3.~~[座號]~~ 沒有順序性
## 4. Tuple簡化版字典
- 1. ()小括號
字典{"name":"Elwing","height":175,"weight":75}
字典拿東西[key]
Tuple:("Elwing",175,75)
Tuple拿東西[0]="Elwing"
Tuple拿東西[1]=175
---
帶入物件導向觀念
import random
def generate_ticket():
ticket = set()
while len(ticket) <7:
n=random.randint(1,48)
ticket.add(n)
return ticket
prize = generate_ticket()
print("頭獎彩券", prize)
times=0
while True:
lottery = generate_ticket()
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