Python 错误 "function not defined"
Python error "function not defined"
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()
当我 运行 程序打印出洗牌后的卡片时,它给了我一个错误 drawCard(shuffledDeck)
说未定义洗牌的牌组。
这是输出:
['8 D', '4 D', 'AS', '8 H', '4 H', '2 S', 'QC', 'QS', '3 D', 'AC', '9 S', 'JS', 'QK', '2 C', '9 H', '5 H', 'JC', '2 D', '6 C', 'KS', '9 D', '9 C', '5 C', 'AH', '2 H', 'AD', '7 D', 'KC', '6 D', 'KD', '3 S', '5 S', '7 H', 'JH', '7 S', '6 H', '7 C', 'JK', '8 S', '6 S', '3 H', '4 S', '4 C', 'QH', '5 D', 'KH', '3 C', '8 C']
Traceback (most recent call last):
File "E:\University of Utah\Fall 2015\EAE 1410\Homework\Assignment 5\HW05_Blackjack.py", line 71, in
main()
File "E:\University of Utah\Fall 2015\EAE 1410\Homework\Assignment 5\HW05_Blackjack.py", line 66, in main
drawCard(shuffledDeck)
NameError: name 'shuffledDeck' is not defined
在函数内部声明的变量是该函数的局部变量(如果您认为它可以作为全局变量使用)。你 return 套牌,但不要保存它。我认为 "def main():" 的第一行应该是:
shuffledDeck = shuffledCards()
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()
当我 运行 程序打印出洗牌后的卡片时,它给了我一个错误 drawCard(shuffledDeck)
说未定义洗牌的牌组。
这是输出:
['8 D', '4 D', 'AS', '8 H', '4 H', '2 S', 'QC', 'QS', '3 D', 'AC', '9 S', 'JS', 'QK', '2 C', '9 H', '5 H', 'JC', '2 D', '6 C', 'KS', '9 D', '9 C', '5 C', 'AH', '2 H', 'AD', '7 D', 'KC', '6 D', 'KD', '3 S', '5 S', '7 H', 'JH', '7 S', '6 H', '7 C', 'JK', '8 S', '6 S', '3 H', '4 S', '4 C', 'QH', '5 D', 'KH', '3 C', '8 C']
Traceback (most recent call last): File "E:\University of Utah\Fall 2015\EAE 1410\Homework\Assignment 5\HW05_Blackjack.py", line 71, in main() File "E:\University of Utah\Fall 2015\EAE 1410\Homework\Assignment 5\HW05_Blackjack.py", line 66, in main drawCard(shuffledDeck) NameError: name 'shuffledDeck' is not defined
在函数内部声明的变量是该函数的局部变量(如果您认为它可以作为全局变量使用)。你 return 套牌,但不要保存它。我认为 "def main():" 的第一行应该是:
shuffledDeck = shuffledCards()