[英]Code execution shell vs Jupyter Notebook
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.
我遇到的情况是,相同的代码在shell中完美执行,而在Jupiter中有一些问题。
In Jupyter Notebook it tends to stop, usually at the beginning of the loop or before user input. 在Jupyter Notebook中,它倾向于停止,通常在循环开始时或在用户输入之前停止。 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.
基本上,该问题发生在应该执行hit_or_stand的游戏开始时,或者发生在repeat_game()函数级别。
# 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.
您应该共享笔记本代码,因此,我可以为您解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.