在主体中使用函数的基本概念
Basic concept of using functions in main body
根据我的理解,当我应该 运行 主体中的 while 循环并调用 hit_or_stand(deck,hand) 函数时,代码应该 运行 无限次当我选择 h 时围绕 while 循环(在函数中),但令我惊讶的是以下代码 运行 以下列方式:
1- Asks for h or s ?
I type h
2- show_some(player_hand,dealer_hand)
3-if player_hand.value > 21:
player_busts(player_hand,dealer_hand,player_chips)
break
你不认为代码应该 运行 无限期地围绕第 1 步,因为我总是选择 h 并且根据我的理解它应该保留在 hit_or_stand() 函数的循环中。
def hit_or_stand(deck,hand):
global playing
while True:
x = input("Would you like stand or hit? Enter 'h' or 's'")
if x[0].lower() == 'h':
hit(deck,hand)
elif x[0].lower() == 's':
print("player stands. Dealer is playing. ")
playing = False
else:
print("Sorry, please try again.")
continue
break
正文
while playing: # recall this variable from our hit_or_stand function
hit_or_stand(deck,player_hand)
show_some(player_hand,dealer_hand)
#if player hand exceeds 21, run player busts() and break out of the loop
if player_hand.value > 21:
player_busts(player_hand,dealer_hand,player_chips)
break
break
指令取消了您的 while 循环。 (https://docs.python.org/2.0/ref/break.html)。
hit_or_stand
函数中的 while True
循环只会 运行 一次,因为最后的 break
语句。而主体中的循环将在到达 if
语句时退出。
根据我的理解,当我应该 运行 主体中的 while 循环并调用 hit_or_stand(deck,hand) 函数时,代码应该 运行 无限次当我选择 h 时围绕 while 循环(在函数中),但令我惊讶的是以下代码 运行 以下列方式:
1- Asks for h or s ?
I type h
2- show_some(player_hand,dealer_hand)
3-if player_hand.value > 21:
player_busts(player_hand,dealer_hand,player_chips)
break
你不认为代码应该 运行 无限期地围绕第 1 步,因为我总是选择 h 并且根据我的理解它应该保留在 hit_or_stand() 函数的循环中。
def hit_or_stand(deck,hand):
global playing
while True:
x = input("Would you like stand or hit? Enter 'h' or 's'")
if x[0].lower() == 'h':
hit(deck,hand)
elif x[0].lower() == 's':
print("player stands. Dealer is playing. ")
playing = False
else:
print("Sorry, please try again.")
continue
break
正文
while playing: # recall this variable from our hit_or_stand function
hit_or_stand(deck,player_hand)
show_some(player_hand,dealer_hand)
#if player hand exceeds 21, run player busts() and break out of the loop
if player_hand.value > 21:
player_busts(player_hand,dealer_hand,player_chips)
break
break
指令取消了您的 while 循环。 (https://docs.python.org/2.0/ref/break.html)。
hit_or_stand
函数中的 while True
循环只会 运行 一次,因为最后的 break
语句。而主体中的循环将在到达 if
语句时退出。