I have a general question regarding code execution. I have a case when the same code is executed perfectly in shell and has some issues in Jupiter.
In Jupyter Notebook it tends to stop, usually at the beginning of the loop or before user input. Nothing happens then but the kernel is busy. Same part of the code sometimes works and sometimes doesn't.
Program is very simple so that should not be an issue. Have you ever faced similar issues? Can it be because I haven't split the code into few cells? Many thanks in advance!
EDIT: I've added the full code as I haven't managed to re-create a problem on a smaller sample. Basically the problem occurs either at the very start of the game when hit_or_stand should be executed or at the repeat_game() function level.
# import
import random
# define classes
deck_colors = ["Hearts", "Spades", "Clubs", "Diamonds"]
deck_ranks = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"]
deck_values = {"Two" : 2, "Three" : 3, "Four" : 4, "Five" : 5, "Six" : 6, "Seven" : 7, "Eight" : 8, "Nine" : 9,
"Ten" : 10, "Jack" : 10, "Queen" : 10, "King" : 10, "Ace" : 11}
class Card():
def __init__(self, color, rank):
self.color = color
self.rank = rank
self.value = deck_values[rank]
def __str__(self):
return self.rank + ' of ' + self.color
class Deck():
def __init__(self):
self.deck = []
for color in deck_colors:
for rank in deck_ranks:
self.deck.append(Card(color, rank))
def __str__(self):
deck_description = ''
for card in self.deck:
deck_description += card.__str__() + '\n'
return "\nCurrent deck:\n" + deck_description
#shuffle deck before the game
def shuffle(self):
random.shuffle(self.deck)
# pop last card to pass it on to players
def pop_card(self):
return self.deck.pop()
class Hand():
def __init__(self, name):
self.cards_in_hand = []
self.value = 0
self.aces = 0
self.name = name
def add_card(self, deck):
new_card = deck.pop_card()
self.cards_in_hand.append(new_card)
self.value += new_card.value
if new_card.rank == "Ace":
self.aces += 1
while self.aces > 0:
if self.value > 21:
self.value -= 10
self.aces -=1
else:
break
class Chips():
def __init__(self):
self.value = 100
self.bet = 0
def take_bet(self):
while True:
bet_value = input(f"You have {self.value} chips. What's your bet? ")
try:
bet_value = int(bet_value)
if bet_value > 0 and bet_value <= self.value:
self.bet = bet_value
self.value -= bet_value
break
elif bet_value > self.value:
print("You don't have enough chips.")
continue
else:
print(f"Choose correct value (1-{self.value})")
continue
except:
print(f"You have to choose correct number (1-{self.value})!")
def win_bet(self):
self.value += (self.bet * 2)
self.bet = 0
def lose_bet(self):
self.bet = 0
def draw(self):
self.value += self.bet
self.bet = 0
# define functions
def show_some_cards(player_hand, dealer_hand):
player_str = ''
for card in player_hand.cards_in_hand:
player_str += f"\n{card.__str__()} "
print("Player's cards:", player_str)
dealer_str = ' '
for card in dealer_hand.cards_in_hand[1:]:
dealer_str += f"\n{card.__str__()} "
print("\nDealer's cards:\n<hidden card>", dealer_str)
def show_all_cards(player_hand, dealer_hand):
player_str = ''
for card in player_hand.cards_in_hand:
player_str += f"\n{card.__str__()} "
print("Player's cards:", player_str)
print("Cards value:", player_hand.value)
dealer_str = ' '
for card in dealer_hand.cards_in_hand:
dealer_str += f"\n{card.__str__()} "
print("\nDealer's cards:", dealer_str)
print("Cards value:", dealer_hand.value)
def hit_or_stand(player1_hand, player2_hand, deck, chips):
global dealer_action
global busted_before
while True:
print('\n'*100)
print("Here're the cards\n")
show_some_cards(player1_hand, player2_hand)
action = input("Do you want another card? (Y/N)")
if action.upper() == "Y":
player1_hand.add_card(deck)
if player_hand.value > 21:
busted(player_hand, dealer_hand, chips)
dealer_action = False
busted_before = True
break
continue
elif action.upper() == "N":
break
else:
print("Choose correct answer (Y/N)")
continue
def dealer_playing(player1_hand, player2_hand, deck, chips):
global busted_before
while True:
print('\n'*100)
print("Here're the cards\n")
show_some_cards(player1_hand, player2_hand)
if player2_hand.value < 17:
player2_hand.add_card(deck)
if player2_hand.value > 21:
busted(dealer_hand, player_hand, chips)
busted_before = True
break
continue
else:
break
def busted(current_hand, other_hand, chips):
print('\n'*100)
if current_hand.name == "Player":
show_all_cards(current_hand, other_hand)
chips.lose_bet()
print(f"Player busted! You now have only {chips.value} chips.")
elif current_hand.name == "Dealer":
show_all_cards(other_hand, current_hand)
chips.win_bet()
print(f"Dealer busted! You now have {chips.value} chips.")
else:
print("Something went wrong! (busted function)")
def check_winners(player1_hand, player2_hand, chips):
print('\n'*100)
if player1_hand.value > player2_hand.value:
show_all_cards(player1_hand, player2_hand)
chips.win_bet()
print(f"Player won! You now have {chips.value} chips.")
elif player1_hand.value < player2_hand.value:
show_all_cards(player1_hand, player2_hand)
chips.lose_bet()
print(f"Dealer won! You now have only {chips.value} chips.")
elif player1_hand.value == player2_hand.value:
show_all_cards(player1_hand, player2_hand)
chips.draw()
print(f"It's a draw! You still have {chips.value} chips.")
def repeat_game(chips):
global repeat
while True:
repetition = input("Would you like to play again? (Y/N)")
if chips.value > 0:
if repetition.upper() == 'Y':
repeat = True
break
elif repetition.upper() == 'N':
repeat = False
break
else:
print("Choose correct value (Y/N)")
continue
else:
print("You don't'have enough chips to continue.")
repeat = False
break
def start_again():
global new_game
while True:
restart_game = input("Would you like to start completely new game? (Y/N)")
if restart_game.upper() == 'Y':
new_game = True
break
elif restart_game.upper() == 'N':
new_game = False
break
else:
print("Choose correct value (Y/N)")
continue
# play the game
new_game = True
while new_game == True:
repeat = True
player_chips = Chips()
print("Welcome to BlackJack!")
while repeat == True:
print('\n'*100)
#### initialization ###
current_deck = Deck()
current_deck.shuffle()
player_hand = Hand("Player")
player_hand.add_card(current_deck)
player_hand.add_card(current_deck)
dealer_hand = Hand("Dealer")
dealer_hand.add_card(current_deck)
dealer_hand.add_card(current_deck)
#### game_ongoing ###
player_chips.take_bet()
dealer_action = True
busted_before = False
hit_or_stand(player_hand, dealer_hand, current_deck, player_chips)
if dealer_action == True:
dealer_playing(player_hand, dealer_hand, current_deck, player_chips)
if busted_before == False:
check_winners(player_hand, dealer_hand, player_chips)
repeat_game(player_chips)
start_again()
Splitting the code in cells has nothing to do with the issue. Different cells are used so you won't run same code again and again. Notebook and shell have some small difference but the basic idea is same. you should share the notebook code, so, i can help you with your issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.