简体   繁体   English

Python列表似乎没有更新

[英]Python list doesn't seem to be updating

I'm fairly new to python and I'm trying to create blackjack. 我是python的新手,正在尝试创建二十一点。 However, when I'm trying to print out what the player's hand is, I run into a few difficulties. 但是,当我尝试打印出玩家的手牌时,我遇到了一些困难。
This is my code for hitting (drawing a card): 这是我的打(发卡)代码:

def hit(card, deck):
    global money, choice

    choice = input("How much would you like to bet?\n")
    money -= int(choice)
    print("You have decided to bet $" + str(choice))
    card = card.drawCard(deck.deck)
    card.getPt()
    deck.addScore(card)
    deck.addCard(card)
    c = str(card)
    p = str(deck)
    print("You have drawn: " + str(c) + "\n")
    print("The player has:\n" + str(p) + "\n")
    print("Total score:", deck.score)

And this is my code for printing my cards out: 这是我用于打印卡的代码:

def __str__(self):
    for i in range(0, len(self.deck)):
        self.print = self.print + "\n" + str(self.deck[i])
    return self.print

The first thing my code does is draw two cards for the dealer and the player, which runs fine. 我的代码要做的第一件事是为发牌者和玩家抽两张牌,效果很好。 However, after the player draws a card is where it gets a bit wonky. 但是,在玩家抽出一张牌之后,它就会变得有些古怪。 The output is something like this: 输出是这样的:

The player has drawn Card A  
The player has drawn Card B
Total score: number

How much would you like to bet?  
number

You have bet number  
You have drawn Card B

Player has:  
Card A  
Card B  
Card A  
Card B  
Card B  

When I draw a new card, the card doesn't change, it stays the last card I drew. 当我抽出一张新卡时,该卡不会更改,而是保留我绘制的最后一张卡。 Then, when I print my deck, it prints my old deck and my new deck. 然后,当我打印卡座时,它会同时打印我的旧卡座和新卡座。 However, the score is correct, which indicates that my list is only three cards long. 但是,分数是正确的,这表明我的名单只有三张卡。 What is going on, and why is it printing five cards? 怎么回事,为什么要打印五张卡?

Full code 完整代码
Example output 输出示例

Well, to get to the answer in short: you never reset Deck.print. 好吧,简而言之,答案是: 您永远不会重置Deck.print。 So it keeps accumulating at each call to __str__ 因此,每次调用__str__它都会不断累积

But in general this code could be improved significantly Eg your __str__ function is far from being pythonic. 但总的来说,可以大大改善此代码,例如__str__函数远非__str__ Something along the lines of 遵循以下原则

return '\n'.join(self.deck)

would look much better. 看起来会好很多。

In general there is no need to prepend each variable with "self." 通常,没有必要在每个变量前加上“自我”。 if they are used within the function only. 如果仅在函数中使用它们。 In most cases in a class method you either update an object variable (self.something) or return some value from the function, but not both. 在大多数情况下,在类方法中,您要么更新对象变量(self.something),要么从函数中返回一些值,但不能同时返回两者。 There may, of course, be exceptions to this, but it seems in your case this is the rule and not the exception. 当然,可能会有例外,但是在您的情况下,这似乎是规则,而不是例外。

Using globals is also something you should avoid whenever possible. 您还应尽可能避免使用全局变量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM