Python 3.x Python-52扑克牌组速记法。我哪里出错了?
我正在做一个关于编程类(Python)的介绍的作业,我的作业必须抓住52张扑克牌的简写符号,让程序吐出长符号。(即:红心皇后) 只是我被卡住了,不知道自己做错了什么。这是我的密码:Python 3.x Python-52扑克牌组速记法。我哪里出错了?,python-3.x,Python 3.x,我正在做一个关于编程类(Python)的介绍的作业,我的作业必须抓住52张扑克牌的简写符号,让程序吐出长符号。(即:红心皇后) 只是我被卡住了,不知道自己做错了什么。这是我的密码: #taking the shorthand notation of a 52 card playing deck #and converting it to the long notation cardValues = {"A": "Ace", "J": "Jack",
#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")
编写
cardNotation[0:://code>将为您提供整个字符串,而不是一个字母,我认为这是您想要的。您应该为第一个字母编制索引,因此它应该是cardNotation[0]
。这是因为当你在索引中使用冒号时,它会说“从我放在括号中的索引到字符串的末尾”。将同样的推理应用于[1::][/code>,并获得正确的颜色索引。即使对索引进行了更改,我也会得到“未定义”错误或“无效语法”,具体取决于我输入的内容。(所有字母都显示为“未定义”[QS]:带数字的是“无效语法”[6H])这是否意味着我必须为int或str编写if语句?更改:如果len(cardNotation)==2:value=cardNotation[0]color=cardNotation[-1]打印“+cardSuit.get(color)”的cardValues.get(value)+”elif len(cardNotation)==3:value=cardNotation[:1]color=cardNotation[-1]以防万一,您还没有解决它:我将您的代码放入我的python编辑器,结果很好。我对它所做的唯一更改是您的函数签名def转换(cardValues,cardSuit):
,我将其更改为def转换(cardNotation):
cardValues
和cardSuit
未定义,直接取自cardNotation
,因此不应将其用作参数。