Skip to content
Advertisement

Tricks and tips for beginners to shorten and simplify code

I am looking for ways to shorten and simplify my python code. Here is one example of one small rock/paper/scissors game I wrote earlier. The code kinda looks too long to me and I want to try to learn how to shorten my code.

import random
user_wins = 0
comp_wins = 0
game_limit = 0
options = ["rock","paper","scissors"]
print("Welcome to Rock Paper Scissors, This is a Best of 5")

while True:
     user_input = input("Type in Rock/Paper/Scissors or Q to quit: ").lower()
     if user_input == "q":
          break
     elif user_input not in options:
          print("type in a valid word next time")
          continue
     game_limit += 1
     if game_limit == 5 and comp_wins > user_wins:
          print("The game is over, YOU LOST!")
     elif game_limit == 5 and comp_wins < user_wins:
          print("The game is over, YOU WON!")

     random_number = random.randint(0,2)
     comp_input = options[random_number]
     print("Computer picked", comp_input)
     if user_input == "rock" and comp_input == "scissors":
          print("You win")
          user_wins += 1
     elif user_input == "rock" and comp_input == "rock":
          print("its a draw")
     elif user_input == "rock" and comp_input == "paper":

          print("You lose!")
          comp_wins += 1

     if user_input == "scissors" and comp_input == "paper":
          print("You win")
          user_wins += 1
     elif user_input == "scissors" and comp_input == "scissors":
          print("its a draw")
     elif user_input == "scissors" and comp_input == "rock":
          print("You lose!")
          comp_wins += 1

     if user_input == "paper" and comp_input == "rock":
          print("You win")
          user_wins += 1
     elif user_input == "paper" and comp_input == "paper":
          print("its a draw")
     elif user_input == "paper" and comp_input == "scissors":
          print("You lose!")
          comp_wins += 1

Advertisement

Answer

Yes. You could simplify your logic by having compound and/or tests for the player and user. Following is a revised version of your code with some simplified logic.

import random
user_wins = 0
comp_wins = 0
game_limit = 0
options = ["rock","paper","scissors"]
print("Welcome to Rock Paper Scissors, This is a Best of 5")

while True:
     if game_limit >= 5:    # Tweak
          if comp_wins > user_wins:
              print("The game is over, YOU LOST!")
              break
          if comp_wins < user_wins:
              print("The game is over, YOU WON!")
              break
          else:
              print("We go to sudden death!")
              
     user_input = input("Type in Rock/Paper/Scissors or Q to quit: ").lower()
     
     if user_input == "q":
          break
     elif user_input not in options:
          print("type in a valid word next time")
          continue
          
     game_limit += 1

     random_number = random.randint(0,2)
     comp_input = options[random_number]
     print("Computer picked", comp_input)
     if user_input == "rock" and comp_input == "scissors" or user_input == "paper" and comp_input == "rock" or user_input == "scissors" and comp_input == "paper":
          print("You win")
          user_wins += 1
     elif user_input == "rock" and comp_input == "paper" or user_input == "paper" and comp_input == "scissors" or user_input == "scissors" and comp_input == "rock":
          print("You lose!")
          comp_wins += 1
     else:
          print("It's a draw")

This way all of the scenarios where the user can win are in one logical test and all of the scenarios where the computer can win are in another logical test. If neither test is true, then the default would have to be a draw.

Also, I tweaked game limit test because if the computer and user have the same score when the game limit is reached, the test result would be false and the game would then continue on and not stop. So there is a bit of additional code to handle a tie after five rounds.

Give that a try.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement