Ad

How To Debug Why A Value In A Loop Is Wrong In My Python Program

- 1 answer

Everything is correct in my code, but after running when the 2nd loop is executed 2 or times the value picked by system is something else and the value (added/subtracted) afterwards is something else..even in both case the variable used is same....

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

What might be wrong here?

Ad

Answer

The problem is that the scores of the two players are reset to 9 marbles at the top of each run of the inner while loop. So after Mr Bot makes a guess, and the scores are adjusted, the scores are then reset back to 9 each just before the player gets asked to make their guess.

The outer while loop is broken when the player says they don't wish to play any more, the inner while loop is broken when the game currently in progress finishes.

Move the lines that reset P_1 and P_2 so that they are only inside the outer while loop. In other words, replace

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

with

while True:
    P_1=9
    P_2=9
    while True:
        # rest of code omitted...
Ad
source: stackoverflow.com
Ad