Board logo

標題: 亂數 (四) - 抽撲克牌 [打印本頁]

作者: tonyh    時間: 2022-6-18 16:54     標題: 亂數 (四) - 抽撲克牌

  1. from random import shuffle

  2. suit=["♠️", "♥️", "♦️", "♣️"]
  3. order=["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
  4. deck=[]
  5. for i in suit:
  6.     for j in order:
  7.         deck.append(i + j)

  8. shuffle(deck)

  9. for i in range(5):
  10.     print(deck.pop())    #後五張
  11.     #print(deck[i])    #前五張
複製代碼

作者: 郭詩瑜    時間: 2022-6-18 17:40

  1. import random as r

  2. suit=["♠️", "♥️", "♦️", "♣️"]
  3. order=["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
  4. deck=[]
  5. for i in suit:
  6.     for j in order:
  7.         deck.append(i+j)
  8. r.shuffle(deck)

  9. final = []
  10. for i in range (5):
  11.     a = r.choice(deck)
  12.     final.append(a)
  13.     deck.remove(a)
  14. print(f"{final}")
  15. print(deck)
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2