我正在使用python创建一个黑杰克游戏。我已经做了很多关于局部变量,传递,返回的阅读,但是我很困惑为什么main()函数中的print语句以下面的方式工作,但是当我将它更改为
时print "The playerCards are: ", returned_playerCards
它打印'无'如果我有'击中'#39;我的其他功能中的另一张卡。 playerCards列表是全局的吗?当我调用我的playerDecision函数然后调用playerHit函数然后调用playerDecision函数时发生了什么。只要输入所有内容,我就会认为这是错误的做法。我怎么办呢?
from random import randint
import time
A = 11
J = 10
Q = 10
K = 10
cards = [A,2,3,4,5,6,7,8,9,10,J,Q,K]
def deal():
#playerCards = [cards[randint(0,len(cards)-1)],cards[randint(0,len(cards)-1)]]
playerCards = [2,3]
dealerCards = [cards[randint(0,len(cards)-1)],cards[randint(0,len(cards)-1)]]
print "You have been dealt a ", playerCards[0], "and a ", playerCards[1]
time.sleep(1.5)
print "Dealer is showing ", dealerCards[0]
time.sleep(1)
return(playerCards, dealerCards)
def playerDecision(playerCards):
stayhit = raw_input("Would you like to 'stay' or 'hit' >> ")
if stayhit == 'hit':
playerCards = playerHit(playerCards)
elif stayhit == 'stay':
print "playerCards in elif of playerDecision ", playerCards
return(playerCards)
return(playerCards)
def playerHit(playerCards):
playerCards.insert(0, cards[randint(0,len(cards)-1)])
print "You were dealt a ", playerCards[0]
time.sleep(1.5)
print "Your current total is ", sum(playerCards)
time.sleep(1.5)
if sum(playerCards) > 21:
return(playerCards)
else:
print "PlayerCards in else of playerHit are ", playerCards
playerDecision(playerCards)
def main():
playerCards, dealerCards = deal()
returned_playerCards = playerDecision(playerCards)
print "The player cards are: ", playerCards
if __name__ == "__main__":
main()
答案 0 :(得分:0)
猜测,你在哪里
else:
print "PlayerCards in else of playerHit are ", playerCards
playerDecision(playerCards)
您需要return
:
else:
print "PlayerCards in else of playerHit are ", playerCards
return playerDecision(playerCards)
否则函数结束时没有return
语句,相当于return None
。