python-3.x - 将列表从另一个类传递给类内的方法,以便修改所述列表并传回 Python 中的原始类

标签 python-3.x algorithm oop methods

我正在为我的在线投资组合编写一个新颖的 Blackjack 程序,该程序可以随机创建纸牌。

为了不在一轮中创建重复的卡片,我创建了一个列表来存储已经创建的卡片。然后根据 dealed_cards 列表中包含的卡片检查新的随机卡片,如果它是重复的,则再次调用该方法并分配一张新卡片。

我的 dealed_cards 列表在创建回合的类中启动,然后作为列表从一个类传递到另一个类,可以在新一轮游戏开始时重新初始化。但是,该列表未正确传递到分配新卡值的类中的方法。

我尝试传递列表的一些方法是: (self, dealed_cards),我得到错误

TypeError deal_card_out() missing 1 required positional argument: 'dealed_cards'
With (self, dealed_cards = [], *args) 

这至少有效但不一定正确传递列表,当我尝试在修改它之前从方法中打印 dealed_cards 列表时,我得到一个空列表。

使用 (self, *dealed_cards) 这会将列表作为元组返回,并且不会正确传递。最后 (self, dealed_cards = []) 结果:仍然没有从函数内部传入 dealed_cards 列表

这是我为了测试这个方法而从主程序中分离出来的测试代码块。

class deal_card(object):

def __init__(self):
    pass
def deal_card_out(self, dealed_cards = []): 
    print("This is a test print statement at the beginning of this method to test that dealed_cards was passed in correctly.")
    print(dealed_cards)
    card_one_face_value = 'Seven'
    card_one_suit_value = 'Clubs'

    for _ in dealed_cards:
        if card_one_face_value == [_[0]]:
            print(f"This is a test print statement inside the for loop within deal_card out, it willl print out [_[0]] inside this for loop: {[_[0]]}")
            if card_one_suit_value == [_[1]]:
                print("test loop successful")
            else:
                print(f"This is a test print statement inside the for loop within deal_card out, it willl print out [_[0]] inside this for loop: {[_[0]]}") 
                pass
        else:
            print(f"this is a test print statement inside the for loop within deal_card out it will print out dealed_cards[_[1]] to show what is happening inside this loop: {[_[1]]}")
            pass
        dealed_cards.append([card_one_face_value,card_one_suit_value])

        print("This is a test print inside of deal_card_out, it prints list dealed_cards after method modifies the list")
        print(dealed_cards)
        return [dealed_cards,card_one_face_value,card_one_suit_value]

dealed_cards = [['Place','Holder'],['Seven','Clubs']]
print("this is a test print statement outside of the method to test that     dealed_cards is being passed in correctly")
print(dealed_cards)
test_run = deal_card.deal_card_out(dealed_cards)

最佳答案

弄清楚该方法有什么问题。 “self”不需要放在方法定义中。由于某种原因,将 self 置于方法调用中并没有正确传递 dealed_cards 列表。此外,dealed_cards 可以作为 dealed_cards 传递,而不是 dealed_cards = []。 所以新的正确方法定义是 def deal_card_out(dealed_cards): for 循环也有问题,测试语句 if card_one_face_value == [_[0]]: 需要更改为 if card_one_face_value == _[0]: 否则您正在用大约 7 个方括号进行测试,即嵌套列表中的字符串。

关于python-3.x - 将列表从另一个类传递给类内的方法,以便修改所述列表并传回 Python 中的原始类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57966313/

相关文章:

python - 使用列表生成器时 Python 3 中的 pdb 模块中可能存在错误

python - 用于图像缩放的双三次插值

C程序查找onto函数的数量并显示所有函数

javascript - 从 javascript 中的方法传递数据

php - 向急需重构的代码库添加功能

python - 权限错误 : [Errno 13] Permission denied: 'file.mp3'

c++ - 如何从 Python 使用 Microsoft MIP SDK?

algorithm - 猜数字,允许说谎

java - 静态变量和方法是否属于该类的 java.lang.Class 对象?

python - 将参数从类传递给方法 pd.read_sql?