骰子游戏 // 到达时循环未正确退出 GAME_END_POINTS

Dice Game // Loop not exiting properly when reached GAME_END_POINTS

while(user_plcr <= GAME_END_POINTS or computer_plcr <= GAME_END_POINTS):
    print_current_player(is_user_turn)
    user_total = take_turn(is_user_turn,COMPUTER_HOLD)
    user_plcr = user_plcr + user_total
    is_user_turn = get_next_player(is_user_turn)
    print_current_player(is_user_turn)
    computer_total = take_turn(is_user_turn,COMPUTER_HOLD)
    computer_plcr = computer_plcr + computer_total
    report_points(user_plcr,computer_plcr)
    print("\n")
    is_user_turn = get_next_player(is_user_turn)

def take_turn(is_user_turn, COMPUTER_HOLD):
human_score = 0
computer_score = 0
if is_user_turn == True:
    begin = raw_input("roll? [yn]")
    if begin == 'y' or begin == 'Y':
        human_rand = roll_die()
    elif begin == 'n' or begin == 'N':
        is_user_turn = False
        return human_score
    while human_rand != 1:
        human_score = human_score + human_rand
        if human_score != 0:
            human_score = str(human_score)
            human_rand = str(human_rand)
            print("roll: " + human_rand)
            print("Current Score: " + human_score)
            human_rand = int(human_rand)
            human_score = int(human_score)
            again = raw_input("roll again? [yn]")
            print("\n")
            if again == 'y' or again == 'Y':
                human_rand = roll_die()
                continue
            elif again == 'n' or again == 'N':
                is_user_turn = False
                break
        else:
            human_score = 1
            human_rand = str(human_rand)
            print("roll: " + human_rand)
            human_rand = int(human_rand)
            print("You rolled a 1. Turn over.")
            human_score = str(human_score)
            print("Current Score: " + human_score)
            human_score = int(human_score)
            return human_score
    if human_rand == 1:
        human_rand = str(human_rand)
        print("roll: " + human_rand)
        human_rand = int(human_rand)
        print("You rolled a 1. Turn over.")
        human_score = 1
        human_score = str(human_score)
        print("Current Score: " + human_score)
        human_score = int(human_score)
        return human_score
    else:
        return human_score
elif is_user_turn == False:
    computer_rand = roll_die()
    while computer_rand != 1:
        computer_score = computer_score + computer_rand
        if computer_score != 0:
            computer_rand = str(computer_rand)
            print("roll: " + computer_rand)
            computer_rand = int(computer_rand)
            if computer_score <= COMPUTER_HOLD:
                computer_rand = roll_die()
                continue
            else:
                computer_score = str(computer_score)
                print("*computer holds on " + computer_score + "*")
                computer_score = int(computer_score)
                is_user_turn = True
                break
        else:
            computer_score = 1
            computer_rand = str(computer_rand)
            print("roll: " + computer_rand)
            computer_rand = int(computer_rand)
            print("The computer rolled a 1. Turn over.")
            return computer_score
    if computer_rand == 1:
        computer_rand = str(computer_rand)
        print("rollclea " + computer_rand)
        computer_rand = int(computer_rand)
        print("The computer rolled a 1. Turn over.")
        computer_score = 1
        return computer_score
    else:
        return computer_score

The function take_turn() stores a value of the scores. report_points shows the total of scores. The loop should exit once the placer or total persay reached the GAME_END_POINTS. Instead it is letting the computer do a dice roll after the user already won the game. I've removed the if-else statements from the code block to provide a clean slate for debugging

while(user_plcr <= GAME_END_POINTS or computer_plcr <= GAME_END_POINTS):

任一 为真时,这将继续。所以,即使 user_plcr > GAME_END_POINTS,它也会一直持续到 computer_plcr > GAME_END_POINTS。当其中任何一个为假时,您想退出。换句话说,在两个都为真时继续。

while (user_plcr <= GAME_END_POINTS and computer_plcr <= GAME_END_POINTS):

此外,在 while 内,如果用户得分超过 GAME_END_POINTS,计算机仍会运行。所以在允许电脑转弯之前需要先查看用户的分数。

while(user_plcr <= GAME_END_POINTS and computer_plcr <= GAME_END_POINTS):
    print_current_player(is_user_turn)
    user_total = take_turn(is_user_turn,COMPUTER_HOLD)
    user_plcr = user_plcr + user_total
    if user_plcr <= GAME_END_POINTS:
        is_user_turn = get_next_player(is_user_turn)
        print_current_player(is_user_turn)
        computer_total = take_turn(is_user_turn,COMPUTER_HOLD)
        computer_plcr = computer_plcr + computer_total
    report_points(user_plcr,computer_plcr)
    print("\n")
    is_user_turn = get_next_player(is_user_turn)