Question

python rock paper scissors game

Posted December 27, 2020 1.1k views
Python

So I was just making a rock paper scissors game. I’m a beginner coder. I am coding so that there is a limit of scoring and that’s 5. so if either the user or the pc reaches that score the game should end. and also according to the user guess and the pc guess the score increases respectively. So I set up a while loop so that as long as the user score and the pc score is not equal to 5, the game should continue. but if both the user guess and pc guess are not the same, no one scores and it prints tie!!. but after I set up a while loop, it doesn’t print tie whenever both guesses are the same. and also it should also print user scored when the user scores. But it doesn’t do it now. Can someone help me? this is my code:

import random

user_score = int(0)
pc_score = int(0)
score_limit = 5


user_guess: str = input(str("pls enter your guess, rock/paper/scissors:"))


my_list = "rock", "paper", "scissors"
pc_guess = random.choice(my_list)
print("the computer chose", pc_guess)

while user_score and pc_score != score_limit:

    user_guess: str = input(str( "pls enter your guess, rock/paper/scissors:"))

    if pc_guess == "rock" and user_guess == "rock":
        print("Tie!!")

    if pc_guess == "paper" and user_guess == "paper":
        print("Tie!!")

    if pc_guess == "scissors" and user_guess == "scissors":
        print("Tie!!")

    if pc_guess == "paper" and user_guess == "rock":
        print("the computer scores")
        pc_score = pc_score + 1
        print( "the pc score is:", pc_score)

    if pc_guess == "rock" and user_guess == "paper":
        print("yay u scored")
        user_score = user_score + 1
        print( "the user score is:", user_score)

    if pc_guess == "rock" and user_guess == "scissors":
        print("the computer scored")
        pc_score = int(pc_score) + 1
        print("the pc score is:", pc_score)

    if pc_guess == "scissors" and user_guess == "rock":
        print("yay u scored")
        user_score = user_score + 1
        print("the user score is:", user_score)

    if pc_guess == "paper" and user_guess == "scissors":
        print("yay u scored")
        user_score = user_score + 1
        print("the user score is:", user_score)

    if pc_guess == "scissors" and user_guess == "paper":
        print("the computer scored")
        pc_score = int( pc_score) + 1
        print( "the pc score is:", pc_score)

        break
edited by bobbyiliev

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there @anandu4115,

You need to change the condition for the while loop to this:

while user_score != score_limit or pc_score != score_limit:

That way it will run until either the user or the computer reaches the score limit.

Also, you would need to add the pc_guess = random.choice(my_list) at the end of the while loop again so that each iteration of the loop the computer has a different choice.

Hope that this helps!
Regards,
Bobby

  • oh thanks so much i didnt know an “or” command existed in python. bro then i did some modifications at the end.i set it up so that if the user or pc reaches the score limit , it should print who won respectively.it does print it when the pc wins but only does it for the user after he reaches the score limit and then enters his guess once again. it doesnt print the user won when he gets 5 points. can u help me??. anyway thanks for answering.pls help me!! . anyway this is my code:

    import random
    
    user_score = int(0)
    pc_score = int(0)
    score_limit = 5
    
    
    while user_score != score_limit or pc_score != score_limit:
    
        user_guess: str = input(str("pls enter your guess, rock/paper/scissors:"))
    
        my_list = "rock", "paper", "scissors"
        pc_guess = random.choice(my_list)
        print("the computer chose", pc_guess)
    
        if pc_guess == "rock" and user_guess == "rock":
            print ( "Tie!!" )
    
        if pc_guess == "paper" and user_guess == "paper":
            print ( "Tie!!" )
    
        if pc_guess == "scissors" and user_guess == "scissors":
            print ( "Tie!!" )
    
        if pc_guess == "paper" and user_guess == "rock":
            print ( "the computer scores" )
    
            pc_score = pc_score + 1
            print ( "the pc score is:", pc_score )
    
        if pc_guess == "rock" and user_guess == "paper":
            print ( "yay u scored" )
    
            user_score = user_score + 1
            print ( "the user score is:", user_score )
    
        if pc_guess == "rock" and user_guess == "scissors":
            print ( "the computer scored" )
    
            pc_score = int ( pc_score ) + 1
            print ( "the pc score is:", pc_score )
    
        if pc_guess == "scissors" and user_guess == "rock":
            print ( "yay u scored" )
    
            user_score = user_score + 1
            print ( "the user score is:", user_score )
    
        if pc_guess == "paper" and user_guess == "scissors":
            print ( "yay u scored" )
    
            user_score = user_score + 1
            print ( "the user score is:", user_score )
    
        if pc_guess == "scissors" and user_guess == "paper":
            print ( "the computer scored" )
    
            pc_score = int ( pc_score ) + 1
            print ( "the pc score is:", pc_score )
    
            pc_guess = random.choice ( my_list )
    
            if user_score == score_limit:
                print("yay the u won")
    
            elif pc_score == score_limit:
                print("oopsies, the computer won, better luck next time")
    
                break
    
    
    edited by bobbyiliev
    • Hi there,

      This looks quite good! You just need to move the last if else statement out of the previous if statement like this:

      import random
      
      user_score = int(0)
      pc_score = int(0)
      score_limit = 5
      
      
      while user_score != score_limit or pc_score != score_limit:
      
          user_guess: str = input(str("pls enter your guess, rock/paper/scissors:"))
      
          my_list = "rock", "paper", "scissors"
          pc_guess = random.choice(my_list)
          print("the computer chose", pc_guess)
      
          if pc_guess == "rock" and user_guess == "rock":
              print ( "Tie!!" )
      
          if pc_guess == "paper" and user_guess == "paper":
              print ( "Tie!!" )
      
          if pc_guess == "scissors" and user_guess == "scissors":
              print ( "Tie!!" )
      
          if pc_guess == "paper" and user_guess == "rock":
              print ( "the computer scores" )
      
              pc_score = pc_score + 1
              print ( "the pc score is:", pc_score )
      
          if pc_guess == "rock" and user_guess == "paper":
              print ( "yay u scored" )
      
              user_score = user_score + 1
              print ( "the user score is:", user_score )
      
          if pc_guess == "rock" and user_guess == "scissors":
              print ( "the computer scored" )
      
              pc_score = int ( pc_score ) + 1
              print ( "the pc score is:", pc_score )
      
          if pc_guess == "scissors" and user_guess == "rock":
              print ( "yay u scored" )
      
              user_score = user_score + 1
              print ( "the user score is:", user_score )
      
          if pc_guess == "paper" and user_guess == "scissors":
              print ( "yay u scored" )
      
              user_score = user_score + 1
              print ( "the user score is:", user_score )
      
          if pc_guess == "scissors" and user_guess == "paper":
              print ( "the computer scored" )
      
              pc_score = int ( pc_score ) + 1
              print ( "the pc score is:", pc_score )
      
              pc_guess = random.choice ( my_list )
      
          if user_score == score_limit:
              print("yay the u won")
      
              break
      
          elif pc_score == score_limit:
              print("oopsies, the computer won, better luck next time")
      
              break
      

      Hope that this helps!
      Regards,
      Bobby