python - 看不到更新变量
问题描述
我正在开发一个控制台二十一点游戏,我无法检查是否是玩家轮流。它以前工作过,但突然间我看不到让它工作
这是控制是否轮到玩家的代码:
#first I declare the variable (p for the players turn and d for the dealer)
current_turn = "p"
#then i loop through the choice function until the current_turn is not equal to "p"
while current_turn == "p":
choice()
#this is how that function looks
def choice():
if player_bust() == True:
print("YOU BUSTED PLEASE TRY AGAIN")
exit()
turn_choice = input(
"What would you like to do (h for Hit, s for Stay): ")
if turn_choice == "h":
hit()
elif turn_choice == "s":
print("Passing turn to dealer")
current_turn = "d"
else:
print("INVALID CHOISE, PLEASE TRY AGAIN")
exit()
#NOTE THAT THE ELIF STATEMENT WHERE THE TURN CHOISE IS s. current_turn is greyed out when assigning it
完整代码:
import random
def PlayGame(numdecks):
current_turn = "p"
def new_deck():
std_deck = [
# 2 3 4 5 6 7 8 9 10 J Q K A
2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
]
# add more decks
std_deck = std_deck * numdecks
random.shuffle(std_deck)
return std_deck[:]
deck = new_deck()
player_cards = []
dealer_cards = []
def setup():
player_cards.append(deck.pop(0))
dealer_cards.append(deck.pop(0))
player_cards.append(deck.pop(0))
dealer_cards.append(deck.pop(0))
if sum(player_cards) > 21:
player_bust()
print(f"Visable dealer card: {dealer_cards[1]}")
print(f"Your cards : {sum(player_cards)}")
if sum(player_cards) == 21:
print("CONGRATS YOU WON!")
exit
def choice():
if player_bust() == True:
print("YOU BUSTED PLEASE TRY AGAIN")
exit()
turn_choice = input(
"What would you like to do (h for Hit, s for Stay): ")
if turn_choice == "h":
hit()
elif turn_choice == "s":
print("Passing turn to dealer")
current_turn = "d"
else:
print("INVALID CHOISE, PLEASE TRY AGAIN")
exit()
def show_card():
print(f"Dealer cards: {sum(dealer_cards)}")
print(f"Your cards: {sum(player_cards)}")
def hit():
print(f"You picked up {deck[0]}")
player_cards.append(deck.pop(0))
player_bust()
show_card()
choice()
def player_bust():
if sum(player_cards) > 21:
for index, card in enumerate(player_cards, start=0):
if card == 11:
player_cards[index] = 1
if sum(player_cards) > 21 and 11 in player_cards:
player_bust()
if sum(player_cards) > 21:
return True
return False
setup()
while current_turn == "p":
choice()
上面的所有代码都在 PlayGame 函数中,如果代码看起来很乱,我也很抱歉
解决方案
推荐阅读
- c++ - 如何删除属于存储在向量中的对象的指针?
- python - 我在运行程序时遇到问题(运行 model.fit)
- php - 关于查询异常 laravel 的格式错误消息
- ios - 如何在 macOS 应用程序的 UIKit 中填充窗口
- java - 无法使用 Selenium(java) 从网站获取错误消息并在 Eclipse 控制台中打印
- r - 检查 R data.frame 列中另一列中的相等值
- html - html中最小化和最大化按钮的字体
- javascript - 如何将 pdf 文件从 URL 转换为 base64 编码字符串
- ios - 快速获取文件 Firestore 干扰返回
- javascript - 过滤数组值 - return 语句不会结束函数