Как эта часть функции снова выполняется и изменяется значения?Python

Программы на Python
Ответить
Anonymous
 Как эта часть функции снова выполняется и изменяется значения?

Сообщение Anonymous »

Я выполняю простое обучающее упражнение по созданию игры в блэкджек из командной строки. Большую часть времени кажется, что почти все работает. У меня есть странная ошибка, которую я пока не смог выяснить. В функции FinalCheck иногда после того, как она выдаст ответ о том, кто выиграл (в большом блоке if/elif), она будет повторять строку print(f"Final Score (...)"), и comp_score каждый раз будет меньше, на разную величину. Иногда этого не происходит. Иногда это так. Когда это происходит, обычно это происходит в 2–4 раза, и каждый раз показатель comp_score снижается на разную величину, обычно в 3–10 раз. Я знаю, что FinalCheck больше не запускается, потому что больше ничего из него не печатает. Я не знаю, что еще искать.

import random

card_deck = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

# boolean starter key
start = input("Would you like to play blackjack? Y or N ")

player_deck = []
comp_deck = []
ace11 = 11
game_over = False

def PlayAgain():
global player_deck
global comp_deck
global game_over

new_game = input("Would you like to play again? Y or N ")

if new_game.lower() == "y":
player_deck = []
comp_deck = []
game_over = False
NewRound()
elif new_game.lower() == "n":
print("Well there you go. ")
else:
return

def NewRound():
# dealer delivers the first round of cards to the player and dealer
global game_over
global player_deck

print("New Round: Here are your cards. ")

FirstDeal()
Check()

while game_over == False:
if player_deck[0] == player_deck[1]:
HSS()
else:
HS()

PlayAgain()

def HS():
choice = input("Enter H for hit, or S for stand. ").lower()
if choice == "h":
Hit()
elif choice == "s":
Stand()
else:
print("Sorry, I didn't catch that. Try again. ")
HS()

def HSS():
choice = input("Enter H for hit, T for split, or S for stand. ").lower()
if choice == "h":
Hit()
elif choice == "s":
Stand()
elif choice == "t":
Split()
else:
print("Sorry, I didn't catch that. Try again. ")
HSS()

def FirstDeal():
global player_deck
global comp_deck

# rand num of set of cards for each player
player_deck.append(random.choice(card_deck))
player_deck.append(random.choice(card_deck))
p1 = int(player_deck[0])
p2 = int(player_deck[1])

comp_deck.append(random.choice(card_deck))
comp_deck.append(random.choice(card_deck))
c1 = int(comp_deck[0])
c2 = int(comp_deck[1])

print(f"Player's cards are {p1} and {p2}. ")
print(f"The sum of your cards is {p1+p2}. ")
print(f"Computer's first card is {c1}. ")

def Check():
# computer checks the status of the table and the game, and moves accordingly
global player_deck
global comp_deck
global ace11
global game_over

player_score = sum(player_deck)
comp_score = sum(comp_deck)
print(f"Your score is {player_score}. ")

if player_score > 21:
if ace11 in player_deck:
player_deck[player_deck.index(11)] = 1
print("Your ace is now a 1 instead of 11. You got another chance. ")
Check()
else:
print("Game over. You passed 21. Dealer wins. ")
game_over = True
elif player_score == 21:
if comp_score == 21:
print("Check Draw. ")
game_over = True
else:
print("Blackjack! You won with 21! ")
game_over = True
else:
print("Good luck, human. ")

def FinalCheck():
# end game check
global player_deck
global comp_deck
global game_over

player_score = sum(player_deck)
comp_score = sum(comp_deck)
print("Entered FinalCheck. ")
Check()

if comp_score < 16:
comp_deck.append(random.choice(card_deck))
print("Computer took another card. ")
FinalCheck()
else:
print("Let's see the cards. ")

print(f"Final Scores: Player score is {player_score}. Dealer score is {comp_score}. ")

if comp_score > 21:
if 11 in comp_deck:
comp_deck[comp_deck.index(11)] = 1
print("Dealer had an Ace. It's been turned to 1. Let's check again. ")
FinalCheck()
else:
print("Player wins by default. ")
game_over = True
elif comp_score > player_score:
print("The house always wins. ")
game_over = True
elif comp_score == player_score:
print("FinalCheck Draw. ")
game_over = True
elif comp_score < player_score:
print("Player wins with a greater score. ")
game_over = True
else:
print("Something went wrong here in FinalCheck. ")

def Hit():
global player_deck

player_deck.append(random.choice(card_deck))
Check()

def Stand():
FinalCheck()

def Split():
print("Banana split. ")

NewRound()


Подробнее здесь: https://stackoverflow.com/questions/798 ... the-values
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»