0

Why is my code getting stuck in the while loop? I am not able to understand why it's happening. This code is running as usual on online code checkers and compilers like Thorny. But while running it on code editor it goes into an infinite while loop. There is some bug which I am not able to remove. Please resolve my problem.

import random WIN_SCORE = 100 DICE_FACES = 6 Position_of_snakes = {17: 7, 54: 34, 62: 19, 98: 79} Position_of_ladders = {3: 38, 24: 33, 42: 93, 72: 84} def starting_title(): print("###### Welcome to Snake & Ladder Game ######") print("###### Lets us Start ######") def players_name(): player1_name = None while not player1_name: player1_name = input("Please enter Player 1 name: ") player2_name = None while not player2_name: player2_name = input("Please enter Player 2 name: ") return player1_name, player2_name def roll_the_dice(): dice_value = random.randint(1, DICE_FACES) return dice_value def snake_and_ladder(player_name, current_position, value_of_dice): previous_position = current_position current_position = current_position + value_of_dice if current_position > WIN_SCORE: return previous_position if current_position in Position_of_snakes: final_position = Position_of_snakes.get(current_position) elif current_position in Position_of_ladders: final_position = Position_of_ladders.get(current_position) else: final_position = current_position return final_position def win_check(player_name, position): if WIN_SCORE == position: print(f"Congratulations!!! {player_name} won the Game") def game_start(): starting_title() player1_position = 0 player2_position = 0 player1_name, player2_name = players_name() while True: player_input_1 = input("Enter 'roll' to Roll the Dice").lower() dice_value = roll_the_dice() player1_position = snake_and_ladder(player1_name, player1_position, dice_value) win_check(player1_name, player1_position) player_input_2 = input("Enter 'roll' to Roll the Dice").lower() dice_value = roll_the_dice() player2_position = snake_and_ladder(player2_name, player2_position, dice_value) win_check(player2_name, player2_position) game_start() 
5
  • You can try to debug using Visual Studio Code Commented Jan 16, 2021 at 6:32
  • 2
    Well, you have an infinite loop, when do you expect it to stop? Commented Jan 16, 2021 at 6:32
  • while True: is an infinite loop. The only way to get out of it is with a break statement, but there isn't one. Commented Jan 16, 2021 at 6:34
  • how can I solved it can you please state Commented Jan 16, 2021 at 6:34
  • win_check() should return True or False depending on whether the player has won. Then you can do if win_check(...): break Commented Jan 16, 2021 at 6:34

2 Answers 2

1

You have while True which is an infinite loop. You need to declare a variable there and get its value updated after every move. You can use your win_check function to return value of condition.

while win_check() 

and in win_check

if WIN_SCORE == position: print(f"Congratulations!!! {player_name} won the Game") return false else return true 
Sign up to request clarification or add additional context in comments.

1 Comment

win_check() is called twice each time through the loop, with different arguments. So you can't use it as the while condition.
0

You need to break out of the loop when a player wins. So win_check() should return whether the player has won, and you can use that in an if statement.

def win_check(player_name, position): if WIN_SCORE == position: print(f"Congratulations!!! {player_name} won the Game") return True else: return False def game_start(): starting_title() player1_position = 0 player2_position = 0 player1_name, player2_name = players_name() while True: player_input_1 = input("Enter 'roll' to Roll the Dice").lower() dice_value = roll_the_dice() player1_position = snake_and_ladder(player1_name, player1_position, dice_value) if win_check(player1_name, player1_position): break player_input_2 = input("Enter 'roll' to Roll the Dice").lower() dice_value = roll_the_dice() player2_position = snake_and_ladder(player2_name, player2_position, dice_value) if win_check(player2_name, player2_position): break 

7 Comments

I tried but the player_input_1 is running again and again
What is the purpose of player_input_1? You never use the variable.
If the player enters roll the random number will get generated and the game starts
I just tested it. The loop stopped when a player won the game. Maybe you're not waiting long enough? It takes quite a few rolls.
That's the purpose of calling input(). You never check whether they typed "roll", so they can type anything. I just kept pressing Return.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.