|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
课后作业第44讲第1题斗地主洗牌,代码如下
- import random
- cards = ["♦1", "♦2", "♦3", "♦4", "♦5", "♦6", "♦7", "♦8", "♦9", "♦10", "♦J", "♦Q", "♦K",
- "♥1", "♥2", "♥3", "♥4", "♥5", "♥6", "♥7", "♥8", "♥9", "♥10", "♥J", "♥Q", "♥K",
- "♣1", "♣2", "♣3", "♣4", "♣5", "♣6", "♣7", "♣8", "♣9", "♣10", "♣J", "♣Q", "♣K",
- "♠1", "♠2", "♠3", "♠4", "♠5", "♠6", "♠7", "♠8", "♠9", "♠10", "♠J", "♠Q", "♠K",
- "☀", "🌙"]
- def fy_shuffle(cards,times):
-
- for i in range(times):
- new = []
- n = len(cards)
- while len(new) < 17 and n > 0:
- k = random.randint(0,n)
- new.append(cards[k])
- cards.pop(k)
- n -= 1
- #print(cards)
- print(new)
- new_cards = fy_shuffle(cards, 3)
- print(cards)
- def dealcards():
- player = []
- a = input("请输入第一位游戏玩家名称:")
- b = input("请输入第二位游戏玩家名称:")
- c = input("请输入第三位游戏玩家名称:")
- player.append(a)
- player.append(b)
- player.append(c)
- k = random.randint(0,3)
- landlord = player[k]
- print("地主是:{landlord}")
- for name in player:
-
- if name != landlord:
- get = " ".join(new)
- print(f"[{name}拿到的牌是:{get}]")
- else:
- get = " ".join(new)
- add = " ".join(cards)
- landlord = get + add
- print(f"[{name}拿到的牌是:{landlord}]")
- dealcards()
复制代码
报错信息如下:
- Traceback (most recent call last):
- File "D:/编程/【课后作业】第044讲:函数(IV)洗牌算法.py", line 26, in <module>
- new_cards = fy_shuffle(cards, 3)
- File "D:/编程/【课后作业】第044讲:函数(IV)洗牌算法.py", line 19, in fy_shuffle
- new.append(cards[k])
- IndexError: list index out of range
复制代码
|
|