Python 随机纸牌游戏
我目前正在做一个随机卡片游戏项目,该程序应该向用户显示5张随机卡片,(第一个问题):我不知道如何随机排列字母列表,下面是我的代码:Python 随机纸牌游戏,python,random,Python,Random,我目前正在做一个随机卡片游戏项目,该程序应该向用户显示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 >>>>
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
您可以使用random.randrange(数字),然后在该位置拉出索引。您可以使用random.randrange(数字),然后在该位置拉出索引。1) 那不行,语法错误太严重了。另外,choice不会起作用,示例是您想要的:
random.sample(hand, 5)
(二)
(一)
那不行,语法错误太严重了。另外,choice不会起作用,示例是您想要的:
random.sample(hand, 5)
(二)
要回答您的第一个问题:
import random
hands = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"]
def play():
cards = random.sample(hands,5)
print "User >>>> ", cards
return cards
def ask_pos(hand):
while 1:
pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
if int(pos_change) < 0 or int(pos_change) > 4:
continue
else:
break
hand[int(pos_change)] = random.choice(hands)
return hand
random。选择[{hand},4]
应导致语法错误。首先,调用函数时使用括号()
,而不是括号[]
。另外,我不明白为什么需要在手上放大括号,因为它已经是一个列表,所以不需要做任何事情
我重新写了你的第二个问题:
import random
hands = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"]
def play():
cards = random.sample(hands,5)
print "User >>>> ", cards
return cards
def ask_pos(hand):
while 1:
pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
if int(pos_change) < 0 or int(pos_change) > 4:
continue
else:
break
hand[int(pos_change)] = random.choice(hands)
return hand
要回答您的第一个问题:
import random
hands = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"]
def play():
cards = random.sample(hands,5)
print "User >>>> ", cards
return cards
def ask_pos(hand):
while 1:
pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
if int(pos_change) < 0 or int(pos_change) > 4:
continue
else:
break
hand[int(pos_change)] = random.choice(hands)
return hand
random。选择[{hand},4]
应导致语法错误。首先,调用函数时使用括号()
,而不是括号[]
。另外,我不明白为什么需要在手上放大括号,因为它已经是一个列表,所以不需要做任何事情
我重新写了你的第二个问题:
import random
hands = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"]
def play():
cards = random.sample(hands,5)
print "User >>>> ", cards
return cards
def ask_pos(hand):
while 1:
pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
if int(pos_change) < 0 or int(pos_change) > 4:
continue
else:
break
hand[int(pos_change)] = random.choice(hands)
return hand
random.choice[{hand},4]
这是你编造的还是有人告诉你在愚人节使用这种语法?这行代码有太多错误,我甚至不知道从哪里开始这段代码无法运行;所以请粘贴运行中的code.random.sample(hand,5),返回一个包含5项hand的随机列表。(导入random,或从random import sample,但是->sample(hand,5)random.choice[{hand},4]
这是你编造的还是有人告诉你在愚人节上使用这种语法的?这行有很多错误,我甚至不知道从哪里开始这段代码不会运行;所以请粘贴运行的代码。random.sample(hand,5),返回一个包含5项随机手牌的列表。(导入随机手牌,或从随机导入样本,然后->样本(手牌,5)首先,谢谢你的帮助。我在我的程序中尝试了你的代码,但是出现了一个错误,它说:NameError:全局名称“hand”没有定义。你想做什么?也就是说,错误从哪里来?我被卡在代码的第二部分,程序无法更改卡的位置。你在输入参数吗?到call函数doask_pos(yourlist)
。确保yourlist
是一个列表,如我在示例中所示是的,但当我运行该程序时,出现一个错误,表示TypeError:ask_pos()正好接受一个参数(给定0)首先,谢谢你的帮助。我在我的程序中尝试了你的代码,但是出现了一个错误,它说:NameError:全局名称“hand”没有定义。你想做什么?也就是说,错误从哪里来?我被卡在代码的第二部分,程序无法更改卡的位置。你在输入参数吗?到call函数doask_pos(yourlist)
。确保yourlist
是一个列表,如我在示例中所示是的,但当我运行该程序时,出现一个错误,表示TypeError:ask_pos()正好接受一个参数(给定0)