Wie läuft dieser Teil der Funktion erneut ab und ändert die Werte?Python

Python-Programme
Anonymous
 Wie läuft dieser Teil der Funktion erneut ab und ändert die Werte?

Post by Anonymous »

Ich mache eine einfache Lernübung, um ein Blackjack-Spiel über die Befehlszeile zu erstellen. Meistens scheint fast alles zu funktionieren. Ich habe einen seltsamen Fehler, den ich noch nicht herausfinden konnte. In der FinalCheck-Funktion wird manchmal, nachdem sie eine Antwort darüber gibt, wer gewonnen hat (im großen if/elif-Block), die Zeile print(f"Final Score (...)") wiederholt, und der comp_score wird jedes Mal um unterschiedliche Beträge niedriger sein. Manchmal klappt es nicht. Manchmal ist es so. Wenn dies der Fall ist, geschieht dies normalerweise zwei- bis viermal, und der comp_score sinkt jedes Mal um einen unterschiedlichen Betrag, normalerweise um 3-10. Ich weiß, dass FinalCheck nicht erneut ausgeführt wird, da nichts anderes daraus gedruckt wird. Ich weiß nicht, wonach ich sonst noch suchen soll.

Code: Select all

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()

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post