我正在为编程类(Python)的入门做作业,而对于我的作业,我必须掌握52张纸牌的简写形式,并使程序吐出长的形式。 (即:红心皇后)
除了我被卡住了,不确定自己在做什么错。这是我的代码:
#taking the shorthand notation of a 52 card playing deck
#and converting it to the long notation
cardValues = {"A": "Ace",
"J": "Jack",
"Q": "Queen",
"K": "King",
"2": "Two",
"3": "Three",
"4": "Four",
"5": "Five",
"6": "Six",
"7": "Seven",
"8": "Eight",
"9": "Nine",
"10": "Ten"}
cardSuit = {"D": "Diamonds",
"H": "Hearts",
"S": "Spades",
"C": "Clubs"}
#grabbing short hand notation
print("Enter card notation: ")
cardNotation = input()
def conversion(cardValues,cardSuit):
if len(cardNotation) == 2:
value = cardNotation[0]
color = cardNotation[-1]
print (cardValues.get(value) + " of " + cardSuit.get(color))
#for 10
elif len(cardNotation) == 3:
value = cardNotation[:1]
color = cardNotation[-1]
print (cardValues.get(value) + " of " + cardSuit.get(color))
#failsafe
else:
print ("INVALID VALUE")
答案 0 :(得分:1)
写cardNotation[0:]
会给您整个字符串,而不只是一个字母,这就是我想您想要的。您应该索引第一个字母,所以它应该是cardNotation[0]
。那是因为当您在索引中使用冒号时,它说“从索引开始,我将括号放在字符串的末尾”。将相同的推理应用于[1:]
,并获得正确的颜色索引。