为了账号安全,请及时绑定邮箱和手机立即绑定

Python在类中附加到数组时复制值

Python在类中附加到数组时复制值

慕后森 2021-08-14 21:13:27
这是我的第一篇 StackOverflow 帖子,如果我做错了什么,请告诉我!我正在尝试用 python 制作纸牌游戏,并被告知使用基于类的系统是最好的。在尝试这样做的同时,当将所有卡片放入一副牌中时,这些类似乎将值复制到代码的 board.cards 部分。#definitions and importsimport randomclass Card:    suit = ""    base = ""class Hand:    cards = []    poweri = 0    powerii = 0class Stack:    cards = []#instantiates classesdeck = Stack()board = Stack()player = Hand()dealer = Hand()#creates ordered empty deckdef newdeck(obj):    for x in ["2","3","4","5","6","7","8","9","A","B","C","D","E"]:        for y in ["C","D","H","S"]:            card = Card()            card.base = x            card.suit = y            obj.cards.append(card)#shuffles deckdef shuffle():    random.shuffle(deck.cards)newdeck(deck)#disabled to make debug easier#shuffle()#prints entire deckprint("\nDeck")for i in range(len(deck.cards)):    print(deck.cards[i].base, deck.cards[i].suit)print(len(deck.cards))#prints entire boardprint("\nBoard")for i in range(len(board.cards)):    print(board.cards[i].base, board.cards[i].suit)板子应该是空的???
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 161 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信