本帖最后由 a1104201 于 2016-1-15 16:30 编辑
你这个题目什么时候能公布下答案我们学习下
排序上不知道咋排
#-*- coding:UTF-8 -*-;
import random as r;
t_type = ['黑桃','红桃','梅花','方块']
pai = {
'黑桃':['A','2','3','4','5','6','7','8','9','10','J','Q','K'],
'红桃':['A','2','3','4','5','6','7','8','9','10','J','Q','K'],
'梅花':['A','2','3','4','5','6','7','8','9','10','J','Q','K'],
'方块':['A','2','3','4','5','6','7','8','9','10','J','Q','K']
}
game = [
{},
{},
{},
{}
]
shu = 1;
def getHuase(t_type):
h_len = len(t_type);
h = h_len-1;
h1 = r.randint(0,h);
huase = t_type;
h1_len = len(pai);
if h1_len==0:
del pai;
del t_type;
huase,h1_len = getHuase(t_type);
h1_len = len(pai);
return (huase,h1_len);
def paixu(a):
for i in a:
print(ord(i));
while shu<=52:
for i in range(4):
huase,h1_len = getHuase(t_type);
h2 = r.randint(0,h1_len-1);
p = pai;
game.setdefault(huase,[]).append(p);
del pai;
shu += 1;
num = 1;
for i in game:
print("第%s位玩家的牌面为:" % num);
for a in i:
#print(sorted(i));
p = reversed(sorted(i));
print(a," ".join(p));
num += 1;
希望能得到指点 zooo 发表于 2016-1-10 00:37
思路:
cardList存52张牌(也可以用循环代替手打。。)
实现
random用的真好{:5_106:} VVFock3r 发表于 2016-1-20 23:27
random用的真好
python太强大,所以才有了这个取巧的方法{:5_109:} 楼主能给出个好的答案参考学习不{:5_96:} herdboy 发表于 2016-1-21 16:00
楼主能给出个好的答案参考学习不
呵呵,我也写了一个,算不上好的答案,共同学习吧。
import random
card = list(range(52))
random.shuffle(card)
def t(a):
list1 = ['A','K','Q','J','10','9','8','7','6','5','4','3','2','1']
return(list1)
play = [[],[],[],[]]
for i in range(4):
play = card
play.sort(reverse = True)
#print(play)
s = []
h = []
d = []
c = []
for j in range(13):
if play > 38:
s.insert(0, t(play-39))
elif play > 25:
h.insert(0, t(play-26))
elif play > 12:
d.insert(0, t(play-13))
else:
c.insert(0, t(play))
print('玩家%s手中纸牌:'%(i+1))
print('黑桃:','%s '*len(s)%tuple(s))
print('红心:','%s '*len(h)%tuple(h))
print('方块:','%s '*len(d)%tuple(d))
print('草花:','%s '*len(c)%tuple(c), '\n')
冬雪雪冬 发表于 2016-1-21 18:22
呵呵,我也写了一个,算不上好的答案,共同学习吧。
{:5_106:}楼主,看了你的程序有一个地方不是很理解。你的那个输出 print('黑桃:','%s '*len(s)%tuple(s)) 是不是对输出的数据排序了? 怎么看的我云里雾里的,能解释一下吗{:5_109:} herdboy 发表于 2016-1-22 10:23
楼主,看了你的程序有一个地方不是很理解。你的那个输出 print('黑桃:','%s '*len(s)%tuple(s) ...
这是将列表输出成字符串格式。
简单的说:
如果 s=
则print('%s '*len(s)%tuple(s))
相当于print('%s %s %s '%(1, 2, 3))
输出:1 2 3 冬雪雪冬 发表于 2016-1-22 11:38
这是将列表输出成字符串格式。
简单的说:
如果 s=
原来是酱紫啊{:5_91:} def printinfo(listinfo,count):
方块 = []
黑桃 = []
红心 = []
草花 = []
for i in listinfo:
if '方块' in i:
方块.append(i)
elif '黑桃' in i:
黑桃.append(i)
elif '草花' in i:
草花.append(i)
else:
红心.append(i)
黑桃 = sorted(黑桃, key=lambda x:num.index(x), reverse=True)
草花 = sorted(草花, key=lambda x:num.index(x), reverse=True)
方块 = sorted(方块, key=lambda x:num.index(x), reverse=True)
红心 = sorted(红心, key=lambda x:num.index(x), reverse=True)
msg = '''
玩家%s的牌
黑桃:%s
红心:%s
方块:%s
草花:%s
''' %(count,黑桃,红心,方块,草花)
print(msg,end='------------')
count = 1
for i in [玩家1,玩家2,玩家3,玩家4]:
printinfo(i, count)
count += 1
不是很懂这段代码中的(方块.append(i)和 key=lambda x:num.index(x))
比如:方块.append(i)中 i 指代的是什么?
key=lambda x:num.index(x)中传入x的参数又是什么?
有哪位大神可以告诉我一下吗? 没有思路
import random
print('下面开始发牌')
card=['黑桃A','黑桃K','黑桃Q','黑桃J','黑桃10','黑桃9','黑桃8','黑桃7','黑桃6','黑桃5','黑桃4','黑桃3','黑桃2','梅花A','梅花K','梅花Q','梅花J','梅花10','梅花9','梅花8','梅花7','梅花6','梅花5','梅花4','梅花3','梅花2','方块A','方块K','方块Q','方块J','方块10','方块9','方块8','方块7','方块6','方块5','方块4','方块3','方块2','红桃A','红桃K','红桃Q','红桃J','红桃10','红桃9','红桃8','红桃7','红桃6','红桃5','红桃4','红桃3','红桃2']
card2=card[:]
cardnum=1
i=1
heitao=[]
hongtao=[]
meihua=[]
fangkuai=[]
playcard=[]
while cardnum!=0:
cardnum=len(card)
if len(playcard)<13:
playcard.append(card.pop(random.randint(0,cardnum-1)))
else:
player='play'+str(i)
player=playcard[:]
print()
print('玩家%s手中纸牌:' % i)
for each in card2:
if each in player:
if '黑桃' in each:
heitao.append(each)
if '红桃' in each:
hongtao.append(each)
if '梅花' in each:
meihua.append(each)
if '方块' in each:
fangkuai.append(each)
print('黑桃:',end=' ')
for a in heitao:
print(a,end=' ')
print()
print('红桃:',end=' ')
for a in hongtao:
print(a,end=' ')
print()
print('梅花:',end=' ')
for a in meihua:
print(a,end=' ')
print()
print('方块:',end=' ')
for a in fangkuai:
print(a,end=' ')
print()
heitao=[]
hongtao=[]
meihua=[]
fangkuai=[]
playcard=[]
i+=1
65230215 发表于 2016-1-10 21:42
你真胆大
页:
1
[2]