|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
"""随机发牌"""
import random
#54张牌
cards = []
for i in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']:
for j in ["♠", "♥", "♣", "♦"]:
cards.append(f"{j}{i}")
cards.append("☼")
cards.append("☽")
#引用上一题的打乱函数
def fy_shuffle(x):
n = len(x)
v = []
for i in range(n):
z = random.randint(0, n-i-1)
v.append(x[z])
x = x[:z] + x[z+1:]
x = v
#定义发牌函数
def game(x, y, z):
people = [x, y, z]
cards = fy_shuffle(cards)#打乱
x_cards = ' '.join(cards[:17])
y_cards = ' '.join(cards[17:34])
z_cards = ' '.join(cards[34:51])
landlord = random.randint(0, 2)
print(f"地主是:{people[landlord]}")
print(" ")
if landlord == 0:
x_cards = ' '.join([x_cards, ' '.join(cards[51:])])
elif landlord == 1:
y_cards = ' '.join([y_cards, ' '.join(cards[51:])])
elif landlord == 2:
z_cards = ' '.join([z_cards, ' '.join(cards[51:])])
print(f"[{x}]拿到的排是:{x_cards}")
print(f"[{y}]拿到的排是:{y_cards}")
print(f"[{z}]拿到的排是:{z_cards}")
#主体程序
player_1 = input("请输入第一位游戏玩家的名称:")
player_2 = input("请输入第二位游戏玩家的名称:")
player_3 = input("请输入第三位游戏玩家的名称:")
print(" ")
game(player_1, player_2, player_3)
这个代码在细节尚还有不少问题,求助,谢谢
您好!根据您提供的代码,我发现了一些问题并进行了修改。以下是修改后的代码:
import random
# 54张牌
cards = []
for i in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']:
for j in ["♠", "♥", "♣", "♦"]:
cards.append(f"{j}{i}")
cards.append("☼")
cards.append("☽")
# 打乱函数
def fy_shuffle(x):
n = len(x)
v = []
for i in range(n):
z = random.randint(0, n-i-1)
v.append(x[z])
x = x[:z] + x[z+1:]
return v
# 定义发牌函数
def game(x, y, z):
people = [x, y, z]
shuffled_cards = fy_shuffle(cards) # 打乱
x_cards = ' '.join(shuffled_cards[:17])
y_cards = ' '.join(shuffled_cards[17:34])
z_cards = ' '.join(shuffled_cards[34:51])
landlord = random.randint(0, 2)
print(f"地主是:{people[landlord]}")
print(" ")
if landlord == 0:
x_cards = ' '.join([x_cards, ' '.join(shuffled_cards[51:])])
elif landlord == 1:
y_cards = ' '.join([y_cards, ' '.join(shuffled_cards[51:])])
elif landlord == 2:
z_cards = ' '.join([z_cards, ' '.join(shuffled_cards[51:])])
print(f"[{x}]拿到的排是:{x_cards}")
print(f"[{y}]拿到的排是:{y_cards}")
print(f"[{z}]拿到的排是:{z_cards}")
# 主体程序
player_1 = input("请输入第一位游戏玩家的名称:")
player_2 = input("请输入第二位游戏玩家的名称:")
player_3 = input("请输入第三位游戏玩家的名称:")
print(" ")
game(player_1, player_2, player_3)
主要修改如下:
在 fy_shuffle 函数中,我添加了 return v 语句,以便将打乱后的列表返回给调用者。
在 game 函数中,我将 cards = fy_shuffle(cards) 修改为 shuffled_cards = fy_shuffle(cards),以便在函数内部使用打乱后的牌列表。
现在,这个代码应该可以正常运行并实现随机发牌功能
求最佳答案
|
|