大家帮忙看看代码有什么问题
课后作业第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)
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
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)
IndexError: list index out of range
应该使用 random.randrange(0, n) 而不是 random.randint(0, n)。random.randrange(a, b) 会生成一个从 a 到 b-1 的随机整数,这样就不会超出列表的索引范围。 fy_shuffle 函数并没有返回任何值(即没有 return 语句),将其结果赋值给 new_cards。由于 fy_shuffle 函数打印了每次抽取的 17 张牌,但没有返回这些牌,所以 new_cards 实际上是 None。 很cool的阳 发表于 2024-8-14 10:36
应该使用 random.randrange(0, n) 而不是 random.randint(0, n)。random.randrange(a, b) 会生成一个从 a...
不是这个问题,def fy_shuffle(cards,times):
new_cards = []
for i in range(times):
new = []
n = len(cards)
while len(new) < 17 and n > 0:
k = random.randrange(0,n)
new.append(cards)
cards.pop(k)
print(new)
new_cards.append(new)
return new_cards代码改过了,还是报错
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)
IndexError: list index out of range while len(new) < 17 and n > 0:
k = random.randint(0,n)
这里要改成n-1
这是第一个。另外第二次开始洗牌,要拿到第一次洗牌的数据吗,还是假洗
再就是习惯不太好,正常是import第三方程序写头上,def的封装函数排前面,把执行的主程序集中放后面 import random
def fy_shuffle(cards_get,times):
new = []
for i in range(times):
n = len(cards_get)
while n > 0:
k = random.randint(0,n-1)
new.append(cards_get)
cards_get.pop(k)
n -= 1
#print(cards)
cards_get = new
#print(new)
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
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}]")
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",
"☀", "🌙"]
num_cards = len(cards)
new_cards = fy_shuffle(cards, 3)
print(cards)
#dealcards()
这样可以解决第一阶段的问题 使用cards_get = new,得到上一轮洗牌的序列,当然其实没什么意义,这里假洗也一样的
import random
def fy_shuffle(cards_get,times):
new = []
cards_new01 = cards_get[:]
for i in range(times):
n = len(cards_new01)
while n > 0:
k = random.randint(0,n-1)
new.append(cards_new01)
cards_new01.pop(k)
n -= 1
#print(cards)
cards_new01 = new[:]
print(new)
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
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}]")
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",
"☀", "🌙"]
num_cards = len(cards)
new_cards = fy_shuffle(cards, 3)
print(cards)
#dealcards()
这样能保留原始的列表,使用[:]取得列表,不然地址引用会改原来的列表,这也是坏习惯 print("地主是:",landlord)
地主这里要这样改,放在括号里是不会出来的 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
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()
对这个后一段的功能看不懂,错误在于1,没有传参数,传参数进去。2.还是序列的长度要减去1
import random
def fy_shuffle(cards_get,times):
cards_new01 = cards_get[:]
new = []
for i in range(times):
n = len(cards_new01)
对dealcards 可以说明下功能吗,确实不理解
while n > 0:
k = random.randint(0,n-1)
new.append(cards_new01)
cards_new01.pop(k)
n -= 1
#print(cards)
cards_new01 = new[:]
print(new)
new = []
return cards_new01
def dealcards(new02):
player = []
a = input("请输入第一位游戏玩家名称:")
b = input("请输入第二位游戏玩家名称:")
c = input("请输入第三位游戏玩家名称:")
player.append(a)
player.append(b)
player.append(c)
k = random.randint(0,2)
landlord = player
print("地主是:",landlord)
for name in player:
if name != landlord:
get = " ".join(new02)
print(f"[{name}拿到的牌是:{get}]")
else:
get = " ".join(new02)
add = " ".join(cards)
landlord = get + add
print(f"[{name}拿到的牌是:{landlord}]")
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",
"☀", "🌙"]
new_cards = fy_shuffle(cards, 3)
#print(cards)
dealcards(new_cards)
页:
[1]