有没有办法将课堂上的文本转换为可打印的?

问题描述 投票:0回答:1

当我运行代码时,一切正常,但 str 不适用于类。当我单击运行时,它给我经销商有(<main.Card对象位于0x0000020D00046720>,<main.Card对象位于0x0000020D00045850>) 您有 [<main.Card 对象位于 0x0000020D00045A90>、<main.Card 对象位于 0x0000020D00046840>],总共 13 个
选择:1 == 留下,2 == 击中 你能帮我吗?我真的需要你的帮助

import random
playerIn = True
dealerIn = True
class Card:
    def __init__(self, face):
        self.face = face
    def __str__(self):
        return str(self.face)
all_cards = []
for face in [2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10,
        'Jack', 'Queen', 'King', 'Ace', 'Jack', 'Queen', 'King', 'Ace', 'Jack', 'Queen', 'King', 'Ace', 'Jack', 'Queen', 'King', 'Ace']:
    all_cards.append(Card(face))



playerhand = []
dealerhand = []

def dealcard(turn):
    card = random.choice(all_cards)
    turn.append(card)
    all_cards.remove(card)

def total(turn):
    total = 0
    Ace_11 = 0
    for card in turn:
        if card in range(11):
            total += card
        elif card in ['King', 'Jack', 'Queen']:
            total += 10
        else:
            total += 11
            Ace_11 += 1
    while Ace_11 and total > 21:
        total -= 10
        Ace_11 -= 1
    return total

def revealdealerhand():
    if len(dealerhand) == 2:
        return dealerhand[0]
    elif len(dealerhand) > 2:
        return dealerhand[0], dealerhand[1]
    
for c in range(2):
    dealcard(dealerhand)
    dealcard(playerhand)

while playerIn or dealerIn:
    print('Dealer had', revealdealerhand())
    print('You have', playerhand, 'for a total of', total(playerhand))
    if playerIn:
        stayORhit = input('Choose: 1 == Stay, 2 == Hit ')
    if total(dealerhand) > 16:
        dealerIn = False
    else:
        dealcard(dealerhand)
    if stayORhit == '1':
        playerIn = False
    else:
        dealcard(playerhand)
    if total(playerhand) >= 21:
        break
    elif total(dealerhand) >= 21:
        break

if total(playerhand) == 21:
    print('You have', playerhand, 'for a total of', total(playerhand), dealerhand, 'for a total of', total(dealerhand))
    print("BlackJack! You win! Congarts.")
elif total(dealerhand) == 21:
    print('You have', playerhand, 'for a total of', total(playerhand), dealerhand, 'for a total of', total(dealerhand))
    print('BlackJack! Dealer wins!')
elif total(playerhand) > 21:
    print('You have', playerhand, 'for a total of', total(playerhand), dealerhand, 'for a total of', total(dealerhand))
    print('You bust! dealer wins!')
elif total(dealerhand) > 21:
    print('You have', playerhand, 'for a total of', total(playerhand), dealerhand, 'for a total of', total(dealerhand))
    print('Dealer busts! You win!')
elif 21 - total(dealerhand) > 21 - total(playerhand):
    print('You have', playerhand, 'for a total of', total(playerhand), dealerhand, 'for a total of', total(dealerhand))
    print("Dealer busts! You win!")
elif 21 - total(dealerhand) < 21 - total(playerhand):
    print('You have', playerhand, 'for a total of', total(playerhand), dealerhand, 'for a total of', total(dealerhand))
    print('You bust! Dealer won!')
else:
    print('You have', playerhand, 'for a total of', total(playerhand), dealerhand, 'for a total of', total(dealerhand))
    print('It is a tie!')
python
1个回答
0
投票

您可以覆盖

__repr__
而不是
__str__

class Card:
    def __init__(self, face):
        self.face = face
    def __repr__(self):
        return str(self.face)
© www.soinside.com 2019 - 2024. All rights reserved.