鱼C论坛

 找回密码
 立即注册
查看: 180|回复: 9

大家帮忙看看代码有什么问题

[复制链接]
发表于 2024-8-14 09:29:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-8-14 10:36:01 | 显示全部楼层
应该使用 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。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-8-14 13:51:25 | 显示全部楼层
很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[k])
            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[k])
IndexError: list index out of range
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-14 14:54:31 | 显示全部楼层
while len(new) < 17 and n > 0:
&#160; &#160;&#160; &#160;&#160; &#160;&#160; &#160;k = random.randint(0,n)

这里要改成n-1
这是第一个。另外第二次开始洗牌,要拿到第一次洗牌的数据吗,还是假洗

再就是习惯不太好,正常是import第三方程序写头上,def的封装函数排前面,把执行的主程序集中放后面
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-14 14:58:14 | 显示全部楼层
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[k])
            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[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}]")

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()

这样可以解决第一阶段的问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-14 14:59:02 | 显示全部楼层
使用cards_get = new,得到上一轮洗牌的序列,当然其实没什么意义,这里假洗也一样的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-14 15:11:08 | 显示全部楼层
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[k])
            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[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}]")

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()

这样能保留原始的列表,使用[:]取得列表,不然地址引用会改原来的列表,这也是坏习惯
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-14 15:38:16 | 显示全部楼层
print("地主是:",landlord)
地主这里要这样改,放在括号里是不会出来的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-14 15:43:39 | 显示全部楼层
def dealcards():
&#160; &#160; player = []
&#160; &#160; a = input("请输入第一位游戏玩家名称:")
&#160; &#160; b = input("请输入第二位游戏玩家名称:")
&#160; &#160; c = input("请输入第三位游戏玩家名称:")
&#160; &#160; player.append(a)
&#160; &#160; player.append(b)
&#160; &#160; player.append(c)
&#160; &#160; k = random.randint(0,3)
&#160; &#160; landlord = player[k]
&#160; &#160; print("地主是:{landlord}")


&#160; &#160; for name in player:
&#160; &#160;&#160; &#160;&#160;&#160;
&#160; &#160;&#160; &#160;&#160;&#160;if name != landlord:
&#160; &#160;&#160; &#160;&#160; &#160;&#160; &#160;get = " ".join(new)
&#160; &#160;&#160; &#160;&#160; &#160;&#160; &#160;print(f"[{name}拿到的牌是:{get}]")
&#160; &#160;&#160; &#160;&#160;&#160;else:
&#160; &#160;&#160; &#160;&#160; &#160;&#160; &#160;get = " ".join(new)
&#160; &#160;&#160; &#160;&#160; &#160;&#160; &#160;add = " ".join(cards)
&#160; &#160;&#160; &#160;&#160; &#160;&#160; &#160;landlord = get + add
&#160; &#160;&#160; &#160;&#160; &#160;&#160; &#160;print(f"[{name}拿到的牌是:{landlord}]")
dealcards()
对这个后一段的功能看不懂,错误在于1,没有传参数,传参数进去。2.还是序列的长度要减去1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-14 15:44:29 | 显示全部楼层
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[k])
            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[k]
    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)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-3 11:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表