如何调试我的 Python 程序中循环中的值为何错误

How to debug why a value in a loop is wrong in my Python program

我的代码中的一切都是正确的,但是在 运行 之后,当第二个循环执行 2 次或系统选择的值是别的东西,之后的值 (added/subtracted) 是别的东西..即使在这两种情况下,使用的变量也是相同的....

import random
player_1='Mr.BOT'
player_2=input('Enter your name : ')
print("                 RULES               \n(1)Each player will be given 9 marbles\n(2)One-by-One each player will hold some of their\n   marbles(it's upto them).\n(3)Other player will have to guess that if the number\n   of marbles that was picked by them is even or odd.\n(4)If the guess made by the other player is correct,\n   the player holding the marbles will have to give him\n   the marbles that he was holding,but if the other player\n   will make a wrong guess that player will now  have to\n   give the number of marbles that the player was holding\n   to that player himself..")
while True:
    while True:
        P_1=9
        P_2=9
        a=random.randint(1, P_1)
        if a%2==0:
            c='even'
        else:
            c='odd'
        d=input('Make your choice (even/odd) : ')
        if d==c:
            print('Your guess was correct....!!')
            P_1-=a
            P_2+=a
        else:
            print('You made a wrong guess....!!')
            P_1+=a
            P_2-=a
        print('Current status.......')
        print(player_1,'       ',player_2)
        print(' ',P_1,'         ',P_2)
        if P_1<=0 or P_2<=0:
            if P_1<=0:
                print(player_1,'LOSE.....')
                break
            else:
                print(player_2,'LOSE.....')
                break
        a1=int(input('How many marbles you want to take in your hands : '))
        while a1>P_2 or a1<=0:
            print('please enter a valid number....!!')
            a1=int(input('How many marbles you want to take in your hands : '))
            if a1>P_2 or a1<=0:
                continue
            break
        b1=a1%2
        if b1==0:
            c1='even'
        else:
            c1='odd'
        D=['even','odd']
        d1=random.choice(D)
        if d1==c1:
            print('Mr.Bot made a correct guess....!!')
            P_2-=a1
            P_1+=a1
        else:
            print('Mr.Bot made a wrong guess....!!')
            P_2+=a1
            P_1-=a1
        print('Current status.......')
        print(player_1,'       ',player_2)
        print(' ',P_1,'          ',P_2)
        if P_1<=0 or P_2<=0:
            if P_1<=0:
                print(player_1,' LOSE.....')
                break
            else:
                print(player_2,' LOSE.....')
                break
    again = input('Wanna play again(y/n) : ')
    if again=='n':
        print('Thanks for playing with me ')
        break

这里可能有什么问题?

问题在于,在内部 while 循环的每个 运行 顶部,两位玩家的分数被重置为 9 个弹珠。因此,在 Bot 先生做出猜测并调整分数后,分数会在玩家被要求猜测之前重新设置为 9。

外层 while 循环在玩家说他们不想再玩时中断,内层 while 循环在当前正在进行的游戏结束时中断。

移动重置 P_1P_2 的行,使它们仅位于外部 while 循环内。换句话说,替换

while True:
    while True:
        P_1=9
        P_2=9
        # rest of code omitted...

while True:
    P_1=9
    P_2=9
    while True:
        # rest of code omitted...