Home > Software design >  Where should I make the modification?
Where should I make the modification?

Time:11-02

enter image description here

This is a python code for Rock Paper Scissor game, played by the user and computer. The score of the user is the variable and the score of the computer is computer_score which are incremented if the conditions fulfilled. The incrementation is not working as player1_score remains 0 even after winning and computer_score is incrementing every five times the loop runs, even when it is losing. Here is the whole code:

import random

hand = ["R","P","S"]


def game():
    
    player1_score = 0
    computer_score = 0

    i=0
    while (i<5):
        print("Enter R for ROCK \nEnter P for PAPER\nEnter S for SCISOR")
        your_hand = input(player1   " picks: ").upper()
        if your_hand not in hand:
            continue
        computer_hand = random.randint(0,2)
        print("Computer picks: ", hand[computer_hand])
        if your_hand == "R" and computer_hand == "S":
            player1_score  = 1 
            print("Your score: ",str(player1_score)   "\n My score: ", str(computer_score))
        elif your_hand == "P" and computer_hand == "R":
            player1_score  = 1
            print("Your score: ",str(player1_score)   "\n My score: ", str(computer_score))
        elif your_hand == "S" and computer_hand == "P":
            player1_score  = 1
            print("Your score: ",str(player1_score)   "\n My score: ", str(computer_score))
        else:
            computer_score  = 1
            print("Your score: ",str(player1_score)   "\n My score: ", str(computer_score))
        i  = 1
    print("Your score: ",str(player1_score)   "\n My score: ", str(computer_score))   
    



print("\t\t\tWELCOME TO ROCK PAPER SCISORS!")

print("\t\t\tRemember this: \n\t\t\t\t SCISORS KILLS PAPER \n\t\t\t\t PAPER KILLS STONE \n\t\t\t\t STONE KILLS SCISORS")
player1 = input("Player 1 Enter your name: ")
print("Player 2: Hello! "   player1  ". I am your COMPUTER!")

print("SO SHALL WE : \n\t1. PLAY \n\t2. EXIT")
start = int(input("Enter the choice: "))
if start == 1:
   game()
else:
    exit()

Where should I make the change?

CodePudding user response:

You failed to assign the expected value to computer_hand.

computer_hand = hand[random.randint(0,2)]

CodePudding user response:

If you change both the print statement print("Computer picks: ", hand[computer_hand]) to print("Computer picks: ", computer_hand) and computer_hand = hand[random.randint(0,2)] then it should work.

  • Related