【发布时间】:2020-01-17 20:23:24
【问题描述】:
我正在为我的在线作品集编写一个新颖的 二十一点 程序,该程序可以随机创建卡片。
为了不在一轮中创建重复的卡片,我创建了一个列表来存储已经创建的卡片。然后根据 dealed_cards 列表中包含的牌检查新的随机牌,如果重复,则再次调用该方法并分配新牌。
我的 dealed_cards 列表在创建回合的类中启动,然后作为列表从一个类传递到另一个类,该列表可以在新一轮游戏开始时重新初始化。但是,该列表未正确传递到类中分配新卡值的方法中。
我尝试传递列表的一些方法是: (自我,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)
【问题讨论】:
标签: python-3.x algorithm oop methods