我有一个关于代码执行的一般问题。我有一个案例,相同的代码在shell中完美执行,并在Jupiter中有一些问题。
在Jupyter Notebook中,它往往会停止,通常是在循环开始时或用户输入之前。什么都没发生,但内核很忙。代码的相同部分有时有效,有时则无效。
程序非常简单,因此不应成为问题。你遇到过类似的问题吗?是不是因为我没有将代码分成几个单元格?提前谢谢了!
编辑:我添加了完整的代码,因为我没有设法在较小的样本上重新创建问题。基本上问题出现在游戏开始时应该执行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()
答案 0 :(得分:0)
拆分单元格中的代码与问题无关。使用不同的单元格,因此您不会一次又一次地运行相同的代码。笔记本和外壳有一些小差异,但基本思路是一样的。你应该分享笔记本代码,所以,我可以帮助你解决问题。