Strange output in Project Rock Paper Scissors

New to Python here. Please help as I am getting a strange output (“built in method…”) for my codes below:

from random import randint

options = ["ROCK", "PAPER", "SCISSORS"]

message = {
  "tie": "Yawn it's a tie!",
  "won": "Yay you won!",
  "lost": "Aww you lost!"
}

def decide_winner(user_choice, computer_choice):
  print "You selected: %s" % user_choice
  print "Computer selected: %s" % computer_choice
  if user_choice == computer_choice:
    print message["tie"]
  elif user_choice == options[0] and computer_choice == options[2]:
    print message[won]
  elif user_choice == options[1] and computer_choice == options[0]:
    print message[won]
  elif user_choice == options[2] and computer_choice == options[1]:
    print message["won"]
  else:
    print message["lost"]
    
def play_RPS():
  user_choice = raw_input("Enter Rock, Paper, or Scissors:")
  user_choice = user_choice.upper
  computer_choice = options[randint(0,2)]
  decide_winner(user_choice, computer_choice)
  
play_RPS()

My result looks like this:

Enter Rock, Paper, or Scissors:rock
You selected: <built-in method upper of str object at 0x7fea7df86510>
Computer selected: ROCK
Aww you lost!

Also, I want to ask why we can’t use the following for user_choice:

user_choice.upper = raw_input("Enter your choice: ")

Many thanks in advance.

.upper is a built-in method of string objects. Methods are functions.

So here:

user_choice = user_choice.upper

you assign to the user_choice a function, because that is what user_choice.upper is. What you should do is this:

user_choice = user_choice.upper()

here we assign result of the method .upper performed on string user_choice to the user_choice.


Also, I want to ask why we can’t use the following for user_choice :
user_choice.upper = raw_input("Enter your choice: ")

Again, user_choice.upper is a method, so here you are overwriting it. Which does not make a much sense and fortunately Python does not allow this (error AttributeError: 'str' object attribute 'upper' is read-only).

1 Like

Are these supposed to be the same: "won" or won ?