Python错误“;“未定义功能”; def shuffledCards(): 卡片列表=[“AC”、“AD”、“AH”、“AS”, “KC”、“KD”、“KH”、“KS”, “JC”、“JK”、“JH”、“JS”, “QC”、“QK”、“QH”、“QS”, “2 C”、“2 D”、“2 H”、“2 S”, "3 C,"3 D,"3 H,"3 S,, “4 C”、“4 D”、“4 H”、“4 S”, “5C”、“5D”、“5H”、“5S”, “6 C”、“6 D”、“6 H”、“6 S”, “7C”、“7D”、“7H”、“7S”, “8C”、“8D”、“8H”、“8S”, "9c,"9d,"9h,"9s", cardAmount=len(卡片列表) shuffledDeck=[] 对于范围内的I(0,信用卡金额): randIndex=randint(0,cardAmount-I-1) shuffledDeck.append(卡片列表[randIndex]) del CardList[randIndex] 返回shuffledDeck ##在这一点上,我们需要抽牌 def抽卡(shuffledDeck): 如果函数中声明的shuffledDeck
变量是该函数的局部变量(如果您认为它可以作为全局变量使用)。你归还了甲板,但没有保存它。我认为“def main():”的第一行应该是:Python错误“;“未定义功能”; def shuffledCards(): 卡片列表=[“AC”、“AD”、“AH”、“AS”, “KC”、“KD”、“KH”、“KS”, “JC”、“JK”、“JH”、“JS”, “QC”、“QK”、“QH”、“QS”, “2 C”、“2 D”、“2 H”、“2 S”, "3 C,"3 D,"3 H,"3 S,, “4 C”、“4 D”、“4 H”、“4 S”, “5C”、“5D”、“5H”、“5S”, “6 C”、“6 D”、“6 H”、“6 S”, “7C”、“7D”、“7H”、“7S”, “8C”、“8D”、“8H”、“8S”, "9c,"9d,"9h,"9s", cardAmount=len(卡片列表) shuffledDeck=[] 对于范围内的I(0,信用卡金额): randIndex=randint(0,cardAmount-I-1) shuffledDeck.append(卡片列表[randIndex]) del CardList[randIndex] 返回shuffledDeck ##在这一点上,我们需要抽牌 def抽卡(shuffledDeck): 如果函数中声明的shuffledDeck,python,shuffle,Python,Shuffle,变量是该函数的局部变量(如果您认为它可以作为全局变量使用)。你归还了甲板,但没有保存它。我认为“def main():”的第一行应该是: def shuffledCards(): CardList = ["AC","AD","AH","AS", "KC","KD","KH","KS", "JC","JK","JH","JS", "QC","QK","QH","QS",
def shuffledCards():
CardList = ["AC","AD","AH","AS",
"KC","KD","KH","KS",
"JC","JK","JH","JS",
"QC","QK","QH","QS",
"2 C", "2 D", "2 H", "2 S",
"3 C", "3 D", "3 H", "3 S",
"4 C", "4 D", "4 H", "4 S",
"5 C", "5 D", "5 H", "5 S",
"6 C", "6 D", "6 H", "6 S",
"7 C", "7 D", "7 H", "7 S",
"8 C", "8 D", "8 H", "8 S",
"9 C", "9 D", "9 H", "9 S",]
cardAmount = len(CardList)
shuffledDeck = []
for I in range(0,cardAmount):
randIndex = randint(0,cardAmount - I - 1)
shuffledDeck.append(CardList[randIndex])
del CardList [randIndex]
return shuffledDeck
## At this point we need to draw cards
def drawCard(shuffledDeck):
if shuffledDeck <= 0:
shuffledCards()
else:
card = shuffledDeck[0]
del shuffledDeck[0]
return card
def main():
shuffledCards()
print(shuffledCards())
drawCard(shuffledDeck)
print(shuffledCards())
print (card)
main()
shuffledeck
是shuffledCards
中的局部变量,在main
之外不可用。可能您需要shuffledeck=shuffledCards()
在main
中,同样的情况也适用于card
谢谢,这回答了我的问题谢谢,这回答了我的问题。
shuffledDeck = shuffledCards()