[Python] Rock paper game wrong results

Discussão em 'Python' iniciado por Stack, Fevereiro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am new to python and have made a rock paper game. I found that the result were wrong sometimes.

    the code of the program:

    moves = ['rock','paper','scissor']
    r,p,s = moves
    result = [[r,r],[r,p],[r,s],
    [p,p],[p,s],[p,r],
    [s,s],[s,r],[s,p]]
    def comp_move():
    import random
    return moves[random.randint(0,2)]
    def game():
    comp_mov = comp_move()
    your_move = input('enter your move: ')
    print('computer played: ' + comp_mov )
    if int(result.index([your_move,comp_mov ])) % 3 == 1 :
    print('you losse')
    elif int(result.index([your_move,comp_mov ])) % 3 == 2 :
    print('you won')
    else :
    print('tie')
    while True:
    game()
    if input('Do you want to play again? (y/n): ') == 'n':
    break


    how to remove this wrong results?

    Example output:

    enter your move: rock
    computer played: rock
    you won
    Do you want to play again? (y/n): y
    enter your move: rock
    computer played: scissors
    you won
    Do you want to play again? (y/n): y
    enter your move: rock
    computer played: rock
    tie
    Do you want to play again? (y/n):


    These are results I have been getting on running this program in first case the expected result should be 'tie' not 'won'.

    How to fix this and what might have caused the issue

    Continue reading...

Compartilhe esta Página