您好我正在通过制作二十一点游戏自己做一个初学者项目。它进展顺利,我甚至可以选择1或11作为Ace值。我的问题是,我正在尝试从卡片中取出卡片给我自己以及当我添加经销商时。
我以为我可以执行.remove()函数来删除它但是在使用http://www.pythontutor.com/
运行程序之后似乎无法正常工作这是我正在使用的套牌代码
from random import randint
def CardDeck():
#sets the card types and values
CardValue = ['Ace','2','3','4','5','6','7','8','9','10','J','Q','K']
CardType = ['Hearts','Spades','Clubs','Diamonds']
Deck = []
Card = randint(0,(len(CardValue )*len(CardType)))
#This iterates all 52 cards into a deck
for i in CardType:
for j in CardValue:
Deck.append(j + ' of ' + i)
temp = Deck[Card]
Deck.remove(Deck[Card]) #this should remove the card from the deck
return temp
答案 0 :(得分:2)
如果您试图从列表中获取项目并将其从列表中删除。这种情况有.pop()
方法:
card = Deck.pop()
这将获取列表中的最后一项,将其分配给card
并将其从列表中删除。
card = Deck.pop(1)
这将从列表中获取第二个(索引1
)项,将其分配给card
并将其从列表中删除。
答案 1 :(得分:0)
谢谢大家的洞察力,我已经弄清楚我认为我现在可以做到这一点,而且我会更好! (也修复了我的惯例)
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
final JFrame frame = new JFrame();
// frame.setUndecorated(true);
int n = 0;
for (String[] mLAF1 : mLAF) {
if (theLAF(mLAF1[1] /*Class LAF*/ )) {
JSlider jsl = new JSlider();
jsl.setMajorTickSpacing(20);
jsl.setMinorTickSpacing(5);
jsl.setPaintLabels(true);
jsl.setPaintTicks(true);
jsl.setSnapToTicks(true);
jsl.setToolTipText(mLAF1[0]);
JPanel jp = new JPanel();
jp.add(jsl);
Dimension d = jpVert.getPreferredSize();
jp.setMinimumSize(d);
jp.setSize(d);
JWindow win = createWindow(frame, jp, mLAF1[0] /*Title LAF*/, n);
n++;
}
}
JPanel jpMain = new JPanel();
jpMain.setBorder(BorderFactory.createLineBorder(new Color(255, 255, 255)));
jpMain.setBackground(new Color(0,0,0));
frame.getContentPane().add(jpMain, BorderLayout.CENTER);+
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(1100, 200);
frame.setVisible(true);
}
});
}
现在我可以将其分解为card和remove_card函数,一切都在点击!
from random import randint
def card_deck():
#sets the card types and values
card_value = ['Ace','2','3','4','5','6','7','8','9','10','J','Q','K']
card_type = ['Hearts','Spades','Clubs','Diamonds']
deck = []
#This iterates all 52 cards into a deck
for i in card_type:
for j in card_value:
deck.append(j + ' of ' + i)
return deck