Issue
Has anyone ever had a problem that Jupyter is busy (stuck) when executing input() inside the while statement? The problem is randomly happening to me. Sometimes the command box is prompted next to the cell, and sometimes the input() box never prompts.
Here is the simpler version of my code:
from IPython.display import clear_output
class game_quit(Exception):
def _render_traceback_(self):
pass
def the_game(player_status = "Offering"):
while True:
clear_output()
print("The Game", flush = True)
print(f"Status: {player_status}", flush = True)
if (player_status == "Offering") or (player_status == "Wrong input!"):
play_offer = input("Would you like to play the game (Y or N)? ")
if play_offer.upper() == "Y":
player_status = "Playing"
play_accepted = True
clear_output()
break
elif play_offer.upper() == "N":
play_accepted = False
clear_output()
break
else:
player_status = "Wrong input!"
clear_output()
continue
else:
player_status = "Playing"
play_accepted = True
clear_output()
break
while play_accepted:
the_play(player_status)
else:
raise game_quit()
def the_play(player_status):
while True:
clear_output()
print("The Game", flush = True)
print(f"Status: {player_status}", flush = True)
pet_offer = input("Do you want to go with a (D)og, a (C)at, or a (P)arakeet? ")
if pet_offer.upper() == "D":
player_pet = "Dog"
clear_output()
break
elif pet_offer.upper() == "C":
player_pet = "Cat"
clear_output()
break
elif pet_offer.upper() == "P":
player_pet = "Parakeet"
clear_output()
break
else:
player_status = "Wrong input!"
clear_output()
continue
while pet_offer:
clear_output()
print(f"Your companion is a {player_pet}", flush = True)
play_again = input("Would you like to continue playing the game (Y or N)? ")
if play_again.upper() == "Y":
play_continue = True
clear_output()
break
elif play_again.upper() == "N":
play_continue = False
clear_output()
break
else:
player_status = "Wrong input!"
clear_output()
continue
if play_continue:
player_status = "Playing"
the_game(player_status)
else:
raise game_quit()
Step to reproduce the problem:
- Execute the code.
the_game()
- The user wants to play the game.
play_offer = input("Would you like to play the game (Y or N)? ")
- The user chooses his pet.
pet_offer = input("Do you want to go with a (D)og, a (C)at, or a (P)arakeet? ")
- The user wants to replay the game.
play_again = input("Would you like to continue playing the game (Y or N)? ")
- The user should expect the text box to choose his pet.
pet_offer = input("Do you want to go with a (D)og, a (C)at, or a (P)arakeet? ")
- A
The problem:
The text box is not showing. The running code is stuck there.
- B
But sometimes:
The text box is showing, and the user can choose his pet.
So far, my only solution is to restart the kernel. Has anyone had a solution for this kind of problem?
Regards,
Ade
Solution
Someone gave me an insight. The problem is because clear_output() asynchronous problem. Then I created this function:
from time import sleep
from IPython.display import clear_output
def refresh_screen():
clear_output()
sleep(0.02)
and replaced all clear_output() with refresh_screen() in my code. The problem is gone. A little delay solved the problem.
Answered By - Ade Syseng
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.