0

问题就在第二行(cards =),据我所知,但我不知道如何解决

import random
cards = [Card('Spade', 10, 'Ace'), Card('Diamond', 5, '5')] 

def play_game():

    print("Welcome to Black Jack! The Game of 21!")
    print()
    x = input("Do you want to play?: ")
    if x == 'y':
        print()
        print("WOOOOO :)")
        print()
        draw_card()
    if x == 'n':
        print("FINE! YOU ARE MEAN!!! :(")


def draw_card():


     a1 = random.randint(0, len(cards) -1)
     print("Player's card: ", a1)
     print()

     x = input("Hit again?: ")
     print()

     if x == 'y':
        a2 = random.randint(0, len(cards) -1)
        print("Player's next card: ", a2)
        print()
        print("Player Total so far: ", a1 + a2)

        print()
        x = input("Hit again?: ")

     if x == 'y':
        a3 = random.randint(0, len(cards) -1)
        sum_ = a1 + a2 + a3
        print()
        print("Player new Total: ", a1 + a2 + a3)
        print()

     b1 = random.randint(0, len(cards) -1)
     print("Dealer's card: ", b1)

     print()
     y = input("Dealer Hit Again?: ")
     print()

     if y == 'y':
        b2 = random.randint(0, len(cards) -1)
        print("Dealer's next card: ", b2)
        print() 
        print("Dealer Total so far: ", b1 + b2)
        print()


     y = input("Dealer Hit Again: ")
     print()

     if y == 'y':
        b3 = random.randint(0, len(cards) -1)
        sum2_ = b1 + b2 + b3
        print("Dealer new Total: ", b1 + b2 + b3)
        print()

     player = sum_

     dealer = sum2_


     if player == dealer:
         print("ITS A TIE")
     if player <= 21:
        print("Player wins IN YOUR FACE COMPUTER")
     if player > 21:
        print("PLAYER YOU LOSE")
     if dealer == 21:
        print("Computer wins IN YOUR FACE PLAYER")
     if dealer >= 22:
        print("Computer FAIL")

class Card(object):
    suit = ''
    name = ''
    value = ''

    def __init__(self, suit, value, name):
        self.suit = suit
        self.value = value
        self.name = name

    def __str__(self):
        return '%s %ss %i' % (self.name, self.suit, self.value)

    def main():
        c = Card('Spade', 8, '8')
        d = Card('Diamond', 10, 'King')

if __name__ == '__main__':
    play_game()
4

1 回答 1

2

Card直到文件稍后才定义。将cards定义移至该行下方class Card

将其放在该if __name__行之前:

cards = [Card('Spade', 10, 'Ace'), Card('Diamond', 5, '5')] 

if __name__ == '__main__':
    play_game()
于 2013-04-22T17:24:19.053 回答