1
\$\begingroup\$

I have to do a project on a game of rock, paper, and scissors, and the code I have so far is a complete code for the game, but my professor does not like it since it is too redundant. Is there anyway to shorten it or make it more simple?

import random

while True:
   bot = random.choice(["Rock", "Paper", "Scissors"]).lower()

   user_choice = input("Choose between Rock, Paper, and Scissors or -1 to exit: \n ").lower()
   if user_choice == bot:
      print("We tied! I chose", bot," and you chose", user_choice)
   elif user_choice == "rock":
      if bot == "paper":
         print("You lose! I chose", bot," and you chose", user_choice , ", Paper beats rock!")
      elif bot == "scissors":
       print("You win! I chose", bot," and you chose", user_choice , ", Rock beat scissors!")
   elif user_choice == "paper":
      if bot == "rock":
         print("You win! I chose",bot," and you chose", user_choice , ", Paper beats rock!")
      elif bot == "scissors:":
         print("You lose! I chose", bot," and you chose", user_choice , ", Scissors beats paper")
   elif user_choice == "scissors":
      if bot == "paper":
         print("You win! I chose", bot," and you chose",user_choice, ", Scissors beats paper")
      elif bot == "rock":
         print("You Lose! I chose", bot," and you chose", user_choice , ", Rock beats scissors")
   else:
      print("Invalid Entry, you typed:", user_choice, ", Please try again: ")

   if user_choice == '-1':
      print("You selected -1 to exit, Goodbye!")
exit()
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Review! How much do you know about creating functions and passing arguments, has this been mentioned yet in the course? \$\endgroup\$ – Mast Feb 28 at 3:01
3
\$\begingroup\$

Welcome to Code review. I want to start out by highlighting a few things that I like about this code.

One thing that I quite liked is handling the tie case first. It is often good to simplify logic as much as possible, and the tie case is a good spot of somewhere that you can simplify, rather than have three more elif lines.

It is also good to see handling of erroneous input from the user. This includes making minor corrections when it is clear what was intended: using the .lower() function to ensure that "Rock" and "rock" work the same. It also includes making clear to the user if they entered something from which you can't guess what they meant. In many ways the mark of a good programmer is how well they handle the unexpected.

There is a minor bug in that though: because the '-1' is after the else it will first complain that the input is invalid, and only then realise that it's the exit signal. (There is another minor bug in that it won't actually call exit() because it needs indenting in to the if, but I think that is probably just a copy paste error to codereview.)

Mast suggested in the comments that you really want to be using functions to help make the code less redundant. In general when you find yourself repeating things it is a good opportunity to think about using a function. For example before your while loop, have this function:

def print_win(bot, user_choice):
    print("You win! I chose",bot," and you chose", user_choice , ", ", user_choice, " beats ", bot, "!")

Then when you win, you can just use

print_win(bot, user_choice)

That way if you ever had to change the message (perhaps, say, to translate the program into another language) you could change all the win messages in one place.

You could also structure the program so that you separate out user input from the actual game, which also makes it easier to improve the game bit. If, for example, you had a function that tells you who won, then it would be easy to track a score over multiple rounds. With your set up, you'd have to update the score separately with every one of those ifs or elifs.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.