2、 编写程序, 4名牌手打牌,计算机随机将52张牌(不含大小鬼)发给4名牌手,在屏幕上显示每位牌手的牌。创建函数gen_pocker()交换牌的顺序,函数getColor()获取牌的花色,函数getValue()获取牌的牌面大小,函数getPuk()获取花色和牌面大小的组合。
第一次写
import random
def gen_pocker(n): #洗牌
random.shuffle(n)
return n
def getColor(n): #获取牌的花色
if n/13==0:
return "梅花"
if n/13==1:
return "方片"
if n/13==2:
return "红桃"
if n/13==3:
return "黑桃"
def getValue(n): #获取牌的大小,0,13,26,39为A 10,23,36,49为J. 11,24,37,50为Q. 12,25,38,51为K
if n%13==0:
return "A"
if n%13==10:
return "J"
if n%13==11:
return "Q"
if n%13==12:
return "K"
else:
return str(n%13)
def getPuk(): #发牌
pocker=[]
for i in range(0,52):
pocker.append(i)
pocker=gen_pocker(pocker)
print(pocker)
person1=[]
person2=[]
person3=[]
person4=[]
for i in range(0,52):
if i%4==0:
person1.append(getColor(pocker[i]) + getValue(pocker[i]))
if i%4==1:
person2.append(getColor(pocker[i]) + getValue(pocker[i]))
if i%4==2:
person3.append(getColor(pocker[i]) + getValue(pocker[i]))
if i%4==3:
person4.append(getColor(pocker[i]) + getValue(pocker[i]))
print(pocker)
print(person1)
print(person2)
print(person3)
print(person4)
getPuk()
此处报错为 person1.append(getColor(pocker[i]) + getValue(pocker[i]))
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
第二次的时候把获取花色改成如下:
if n >= 0 and n <= 12:
return "梅花"
if n >= 13 and n <= 25:
return "方片"
if n >= 26 and n <= 38:
return "红桃"
if n >= 39 and n <= 51:
return "黑桃"
正常运行,请问一下为什么会报错为'NoneType' and 'str'啊