简体   繁体   English

如果我不知道要添加多少变量,如何向类添加更多变量 (python)

[英]How do I add more variables to class if I don't know how many I would add (python)

I'm just making a blackjack game on python.我只是在 python 上制作一个二十一点游戏。 So I have a player class and I set player's variables as the cards the user gets.所以我有一个玩家类,我将玩家的变量设置为用户获得的卡片。 So you start with 2 cards.所以你从2张牌开始。 But as the user wants to get more cards I have to add new cards to the class.但是当用户想要获得更多卡片时,我必须向班级添加新卡片。 How would I do that?我该怎么做? It seems redundant to make a bunch of unassigned variables and assign them as the user gets more cards.制作一堆未分配的变量并在用户获得更多卡片时分配它们似乎是多余的。

class Player:
    def __init__(self, pcard1, pcard2):
        self.Pcard1 = pcard1
        self.Pcard2 = pcard2

    def player_starting_cards(self):
        print("Your cards are " + str(self.Pcard1) + " and " + str(self.Pcard2))

Your best bet would be to have a list of cards and append to it.你最好的办法是有一个卡片列表并附加到它上面。 Something like:就像是:

class Player:
    def __init__(self, *cards):
        self.cards = cards

    def player_starting_cards(self):
        print("Your cards are " + " and ".join(self.cards))

The proper way to add fields to an object, however is with setattr :向对象添加字段的正确方法是使用setattr

>>> class A():
...     pass
...
>>> a = A()
>>> a.__dict__ # Contain object attributes
{}
>>> setattr(a, "my_attr", 42)
>>> a.__dict__
{'my_attr': 42}

Hope that helps.希望有帮助。

Use some sort of collection type like a list of dict.使用某种集合类型,如字典列表。 These are single things that can hold multiple things.这些是可以容纳多个事物的单一事物。

EG:例如:

class Player:
    def __init__(self, cards):
        self.card_list = cards

    def player_starting_cards(self):
        print("Your cards are {}".format(', '.join(self.card_list)))

player = Player(['qh', '3c'])
player.player_starting_cards()

You may finding yourself wanting a Card class that knows how to order cards from highest value to lowest, or able to collect cards by suit or numerically (3 deuces, or all hearts, for EG).您可能会发现自己想要一个 Card 类,它知道如何从最高值到最低值排序卡片,或者能够按花色或数字收集卡片(EG 为 3 平分,或所有红心)。

您可以使用列表来实现所需的目标,而不是为您可能拥有的每个值分配一个属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我不知道一个人将如何运行它,是否需要在python脚本的顶部添加任何内容? - Do I have to add anything at the top of a python script if I don't know how a person will run it 我怎么知道要在Python中添加多少个反斜杠 - how do I know how many backslash to add in Python 我想在python代码中添加ssl证书,但我不知道如何在代码中添加它 - I want to add ssl certificate in python code but i don't know how to add it in the code 我想在 tkinter 中制作的记事本左侧添加边距,但我不知道该怎么做? - I want to add margin to the left side of notepad I made in tkinter and I don't know how to do it? 我不知道如何在glade / python(快速生成)中向菜单项添加功能 - I don't know how to add a function to a menu Item in glade/python (generated with quickly) 我想将 tkinter 添加到此代码并将执行此操作,但我不知道将 tkinter 变量放在哪里?它们最终会在 while true 中 - I want to add tkinter to this code and will do that but i don't know where to place the tkinter variables?, they will eventually be in the while true 如果我不知道列表中有多少个值,则使用 SQL 语句 - SQL statement if I don't know how many values in list 如何在Python中使用此信息? 我不知道如何使用此数据类型 - How do I use this information in Python? I don't know how to use this data-type 我有一个 python TypeError,我不知道如何修复它 - I have a python TypeError and I don't know how to fix it Python - 如果我不知道扩展名,如何在文件夹中找到文件? - Python - How do I find a file in a folder if I don't know the extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM