Sometimes my code doesn't work and sometimes it does

Viewed 73

I am creating a rock,paper,scissors game but sometimes my output shows and sometimes it doesn't what's seems to be the problem here? So far this is my code:

import random                                                               
                                                                        
round = 1                                                                   
win = 0                                                                     
lose = 0                                                                    
tie = 0                                                                     
                                                                        
                                                                        
while True:                                                                 
    choices = ["rock", "paper", "scissors"]                                 
                                                                        
    computer = random.choice(choices)                                       
                                                                        
    player = None                                                           
                                                                        
    while player not in choices:                                            
        player = input("Rock, paper, or scissors??: ").lower()              
                                                                        
    if player == computer:                                                  
        print("Player : ", player)                                          
        print("Computer: ", computer)                                       
        print("Tie!")                                                       
        tie+=1                                                              
                                                                        
    elif player == "rock":                                                  
        if computer == "paper":                                             
            print("Player : ", player)                                      
            print("Computer: ", computer)                                   
            print("You lose")                                               
            lose+=1                                                         
                                                                        
    elif player == "paper":                                                 
        if computer == "rock":                                              
            print("Player : ", player)                                      
            print("Computer: ", computer)                                   
            print("You win")                                                
            win+=1                                                          
                                                                        
    elif player == "scissors":                                              
        if computer == "rock":                                              
            print("Player : ", player)                                      
            print("Computer: ", computer)                                   
            print("You lose")                                               
            lose+=1                                                         
                                                                        
    elif player == "rock":                                                  
        if computer == "scissors":                                          
            print("Player : ", player)                                      
            print("Computer: ", computer)                                   
            print("You win")                                                
            win+=1                                                          
                                                                        
    elif player == "paper":                                                 
        if computer == "scissors":                                          
            print("Player : ", player)                                      
            print("Computer: ", computer)                                   
            print("You lose")                                               
            lose+=1                                                         
                                                                        
    elif player == "scissors":                                              
        if computer == "paper":                                             
            print("Player : ", player)                                      
            print("Computer: ", computer)                                   
            print("You win")                                                
            win+=1                                                          
    play_again = input("Would you like to play again? (yes/no): ").lower()  
                                                                        
    if play_again != "yes":                                                 
        print("ROUNDS PLAYED: ",round)                                    
        print("TOTAL WINS:",win)                                          
        print("TOTAL LOSES: ",lose)                                       
        print("TOTAL TIME PLAYER TIED WITH COMPUTER: ",tie)               
        break                                                             
    elif play_again == "yes":                                               
        rounds = round + 1                                                  
        print("NEW ROUND, ROUND: ",rounds)                                  
                                                                        
print("Bye, Thanks for playing this program")

now if I were to choose paper it won't work it will show me something like this: (also I have realized that my rounds doesn't count.)

 Rock, paper, or scissors??: paper
 Would you like to play again? (yes/no): yes
 NEW ROUND, ROUND:  2
 Rock, paper, or scissors??: paper
 Would you like to play again? (yes/no): yes
 NEW ROUND, ROUND:  2
 Rock, paper, or scissors??: scissors
 Player :  scissors
 Computer:  scissors
 Tie!
 Would you like to play again? (yes/no): yes
 NEW ROUND, ROUND:  2
 Rock, paper, or scissors??: rock
 Player :  rock
 Computer:  rock
 Tie!
 Would you like to play again? (yes/no): yes
 NEW ROUND, ROUND:  2
 Rock, paper, or scissors??: paper
 Would you like to play again? (yes/no): no
 ROUNDS PLAYED:  1
 TOTAL WINS: 0
 TOTAL LOSES:  0
 TOTAL TIME PLAYER TIED WITH COMPUTER:  2
 Bye, Thanks for playing this program

I am new to programming any help would be highly appreciated.

3 Answers

You have two different elif block with same conditions. For Example:

elif player == "scissors":
    if computer == "rock":
        print("Player : ", player)
        print("Computer: ", computer)
        print("You lose")
        lose += 1
elif player == "scissors":
    if computer == "paper":
        print("Player : ", player)
        print("Computer: ", computer)
        print("You win")
        win += 1

In this case, if player choose scissors what is gonna happen? Python starts to search for this condition but only the first one will be valid.

Thus, you should construct your elif block like this:

    elif player == "scissors":
        if computer == "paper":
            print("Player : ", player)
            print("Computer: ", computer)
            print("You win")
            win += 1
        elif computer == "rock":
            print("Player : ", player)
            print("Computer: ", computer)
            print("You lose")
            lose += 1

Let's look at your second question.

You have a variable named round and rounds. These are same I guess but you are incrementing rounds by the line rounds = round+1.

Instead, try

round = round+1

or

round+=1

Hope this helps.

Your code is not working sometimes because you have not applied all the condition for example what if player choses paper and computer choses rock.

Also the number of round is not increasing because you are saving it in new variable every time, hence not increasing .

 rounds = round + 1  

above line of code should be

 round = round + 1  

If you put the same condition more than once in any if-elif structure, only the first conditional if/elif branch will satisfy and the latter will never be executed. For example, in your code in the if-else statement, you have written the same condition elif player == "rock" twice.

elif player == "rock":                                                  
    if computer == "paper":                                             
        print("Player : ", player)                                      
        print("Computer: ", computer)                                   
        print("You lose")                                               
        lose+=1                                                         
                                                                        
elif player == "paper":                                                 
    if computer == "rock":                                              
        print("Player : ", player)                                      
        print("Computer: ", computer)                                   
        print("You win")                                                
        win+=1                                                          
                                                                        
elif player == "scissors":                                              
    if computer == "rock":                                              
        print("Player : ", player)                                      
        print("Computer: ", computer)                                   
        print("You lose")                                               
        lose+=1                                                         
                                                                        
elif player == "rock":                                                  
    if computer == "scissors":                                          
        print("Player : ", player)                                      
        print("Computer: ", computer)                                   
        print("You win")                                                
        win+=1                                                          

Say, your player has entered rock and computer has chosen scissors. When the code executes, only the first elif player == "rock" will be evaluated, inside this statement, you only checked if the computer has chosen paper or not. But computer has chosen scissors which you did not check in this statement. So, the interpreter will do nothing and skip the rest of the if-else condition.

To make it work, you need to use only one else statement for the same condition, like this:

elif player == "rock":                                                  
    if computer == "paper":                                             
        print("Player : ", player)                                      
        print("Computer: ", computer)                                   
        print("You lose")                                               
        lose+=1   
    elif computer == "scissors":                                          
        print("Player : ", player)                                      
        print("Computer: ", computer)                                   
        print("You win")                                                
        win+=1 

You need to follow this for all the possible outcomes.

Related