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()
当我运行该程序时,它打印出shuffledCards但是它给我一个错误,drawCard(shuffledDeck)
说没有定义shuffledDeck。
这是输出:
[&#39; 8 D&#39;,&#39; 4 D&#39;,&#39; AS&#39;,&#39; 8 H&#39;,&#39; 4 H&# 39;,&#39; 2 S&#39;,&#39; QC&#39;,&#39; QS&#39;,&#39; 3 D&#39;,&#39; AC&#39;, &#39; 9 S&#39;,&#39; JS&#39;,&#39; QK&#39;,&#39; 2 C&#39;,&#39; 9 H&#39;,&# 39; 5 H&#39;,&#39; JC&#39;,&#39; 2 D&#39;,&#39; 6 C&#39;,&#39; KS&#39;,&#39; 9 D&#39;,&#39; 9 C&#39;,&#39; 5 C&#39;,&#39; AH&#39;,&#39; 2 H&#39;,&#39; AD& #39;,&#39; 7 D&#39;,&#39; KC&#39;,&#39; 6 D&#39;,&#39; KD&#39;,&#39; 3 S&#39 ;&#39; 5 S&#39;,&#39; 7 H&#39;,&#39; JH&#39;,&#39; 7 S&#39;,&#39; 6 H&#39; ,&#39; 7 C&#39;,&#39; JK&#39;,&#39; 8 S&#39;,&#39; 6 S&#39;,&#39; 3 H&#39;, &#39; 4 S&#39;,&#39; 4 C&#39;,&#39; QH&#39;,&#39; 5 D&#39;,&#39; KH&#39;,&# 39; 3 C&#39;,&#39; 8 C&#39;]
追踪(最近一次通话): 文件&#34; E:\犹他大学\ 2015年秋季\ EAE 1410 \家庭作业\作业5 \ HW05_Blackjack.py&#34;,第71行, 主要() 文件&#34; E:\犹他大学\ 2015年秋季\ EAE 1410 \家庭作业\作业5 \ HW05_Blackjack.py&#34;,第66行,主要 的drawcard(shuffledDeck) NameError:name&#39; shuffledDeck&#39;未定义
答案 0 :(得分:2)
在函数内声明的变量是该函数的本地变量(如果您认为它可以作为全局函数使用)。你返回甲板,但不要保存它。我认为&#34; def main()的第一行:&#34;应该是:
shuffledDeck = shuffledCards()