我正在使用一本名为Python编程的书:介绍计算机编程,我在第10章的编程练习中陷入困境。它要求一个程序在用户插入卡片的价值后显示一张扑克牌适合。另外,我应该使用3个方法加上两个构造函数,它们是:
__init__(self, rank, suit):
getRank(self)
getSuit(self)
BJValue(self)
__str__(self)
然而,当我运行它时显示错误..... 这是我的工作:
from random import randrange
class Card:
def __init__(self, rank, suit):# This constructor creates the corresponding card according to their ranks:
self.rank = rank # "d"=diamonts, "c"=clubs, "h"=hearts, or
"s"=spades
self.suit = suit
def getRank(self):# Returns the rank of the card.
ranks = [None, "Ace", "2", "3",
"4", "5", "6", "7", "8",
"9", "King", "Queen", "Jack"]
self.rank = ranks[self.rank]
return self.rank
def getSuit(self):# Returns the suit of the card.
suits = ["diamons", "heart", "club", "spades"]
# TRY TO MAKE THIS PIECE OF CODE MORE ABSTRACT!!!!
if self.suit[0] == "d":
self.suit = suits[0]
elif self.suit[0] == "h":
self.suit = suits[1]
elif self.suit[0] == "c":
self.suit = suits[2]
elif self.suit[0] == "s":
self.suit = suits[3]
return self.suit# A suit in Blackjack means the symbol of the card.
def BJValue(self):# Returns the Blackjack value of a card.
# For example Aces count as 1 and face cards count as 10.
while 0 < self.rank <= 10:
if self.rank == "Ace":
self.rank = 1
self.bjvalue = self.rank
elif self.rank[0] == "King" or self.rank[0] == "Queen" or self.rank[0] == "Jack":
self.rank = 10
self.bjvalue = self.rank
else:
self.bjvalue = self.rank
return self.bjvalue
def __str__(self):# Returns a string that names the card. For example "Ace of Spade".
print("{0} of {1}".format(self.rank, self.suit)
我很抱歉我的英语,但不是我的第一语言。
答案 0 :(得分:1)
您在最后的print
来电中错过了近括号。
答案 1 :(得分:0)
您在最后一行末尾错过了关闭括号,打印方法有一个开头,但最后没有关闭。