为什么我的代码会卡在while循环中?我不能理解为什么会发生这种情况。这段代码像往常一样在像Thorny这样的在线代码检查器和编译器上运行。但是当在代码编辑器中运行它时,它会进入无限的while循环。有一些错误,我不能删除。请帮我解决问题。
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()
发布于 2021-01-16 06:36:42
当玩家获胜时,你需要跳出这个循环。所以win_check()
应该返回玩家是否赢了,您可以在if
语句中使用它。
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
发布于 2021-01-16 06:39:22
你有一个无限循环的while True。您需要在那里声明一个变量,并在每次移动后更新它的值。您可以使用您的win_check函数来返回condition的值。
while win_check()
在win_check中
if WIN_SCORE == position:
print(f"Congratulations!!! {player_name} won the Game")
return false
else
return true
https://stackoverflow.com/questions/65747065
复制相似问题