pythonoutputgame-physicspyscripter

Python Code for Rock, Paper & Scissor Gave Wrong Output


Essence of this code is to depict Rock, Paper and Scissors game using Python language basically with for loop and if...else statements. I used PyScripter to run the code on Python 3.7.2 as engine. The def main() and if __name__ == '__main__' are PyScripter codes for running the machine

import random

def main():
    pass

if __name__ == '__main__':
    main()


tie_sum, comp_sum, human_sum = 0, 0, 0
name = input('Enter your firstname here: ')

for i in range(5):
    tie_sum += tie_sum
    comp_sum += comp_sum
    human_sum += human_sum

    comp_guess = random.randint(1, 3)
    print(f'The computer guess option is {comp_guess}')
    human_guess = int(input('Enter 1 as (rock), 2 as (paper) or 3 as (scissors):'))

    if comp_guess == 1 and human_guess == 3:
        comp_sum += 1
    elif comp_guess == 1 and human_guess is 2:
        human_sum += 1
    elif comp_guess == 2 and human_guess == 3:
        human_sum += 1
    elif comp_guess == 3 and human_guess == 1:
        human_sum += 1
    elif comp_guess == 3 and human_guess == 2:
        comp_sum += 1
    elif comp_guess == 2 and human_guess == 1:
        comp_sum += 1
    else:
        tie_sum += 1

print(f'The number of tie in this game is {tie_sum}')

if comp_sum > human_sum:
    print('The winner of this game is the Computer.')
    print(f'The comp_sum is {comp_sum}')
elif comp_sum < human_sum:
    print(f'The winner of this game is {name}.')
    print(f'The human sum is {human_sum}')
else:
    print('This game ends in tie.')
    print(f'The tie sum is {tie_sum}')

Solution

  • The reason for that is the first three lines in the for loop. You are increasing the sum of computer, human and tie while checking the condition and when you the loop iterates again, it sums up again. Here's the modified code:

    import random
    
    def main():
        pass
    
    if __name__ == '__main__':
        main()
    
    
    tie_sum, comp_sum, human_sum = 0, 0, 0
    name = input('Enter your firstname here: ')
    
    for i in range(5):
        
    
        comp_guess = random.randint(1, 3)
        
        human_guess = int(input('Enter 1 as (rock), 2 as (paper) or 3 as (scissors):'))
        print(f'The computer guess option is {comp_guess}')
        if comp_guess == 1 and human_guess == 3:
            comp_sum += 1
        elif comp_guess == 1 and human_guess == 2:
            human_sum += 1
        elif comp_guess == 2 and human_guess == 3:
            human_sum += 1
        elif comp_guess == 3 and human_guess == 1:
            human_sum += 1
        elif comp_guess == 3 and human_guess == 2:
            comp_sum += 1
        elif comp_guess == 2 and human_guess == 1:
            comp_sum += 1
        else:
            tie_sum += 1
    
    print(f'The number of tie in this game is {tie_sum}')
    
    if comp_sum > human_sum:
        print('The winner of this game is the Computer.')
        print(f'The comp_sum is {comp_sum}')
    elif comp_sum < human_sum:
        print(f'The winner of this game is {name}.')
        print(f'The human sum is {human_sum}')
    else:
        print('This game ends in tie.')
        print(f'The tie sum is {tie_sum}')
    

    Also, there was another modification, the computer guess is supposed to be printed after human's guess. I fixed that too. Hope that helps.