我目前正在做一个随机纸牌游戏项目,程序应该向用户显示 5 张随机纸牌,(第一个问题):我不知道如何随机排列字母列表,这是我的代码:
def play():
hand = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"]
for i in range(len(hand)):
card = random.choice[{hand},4]
print "User >>>> ",card
return card
第二个问题:如果用户想改变卡片的位置。用户应输入编号。的位置变化,那么程序应该随机改变卡。例如:AJ891,用户输入:1,--> A2891。我应该怎么办?这是我的原始代码,但它不起作用
def ask_pos():
pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
while not (pos_change.isdigit()):
print "Your input must be an integer number"
pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
if (pos_change > 4) :
print "Sorry the value has to be between 0 and 4, please re-type"
pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
return pos_change
hand = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"]
for i in range(len(hand)):
card = random.choice[{hand},4]
new = random.choice[{hand},1]
for i in range(len(card)):
if (card[i] == pos_change):
card = card + new
return card