The console always says the user wins, when he should lose and vice versa


#1

Hi all,

my problem is that the console always says the user wins, when he should lose and vice versa. When there should be a tie, it works correctly.
When I swap the number in each line it works. But it´s completely illogical to me and the hints say it should be like in my original code. Here is the "swapped" Code:

  elif user_choice_index == 0 and comp_choice_index == 2:
    print WIN_MSG
  elif user_choice_index == 1 and comp_choice_index == 0:
    print WIN_MSG
  elif user_choice_index == 2 and comp_choice_index == 1:

Here is the complete "original" Code:

 """
This code will ask the user to input either Rock, Paper or Scissor. Randomly decide R/P/S for the computer and then decide and announce who won.
"""
# imports
from random import randint
from time import sleep
# constantly needed variables
options = ["R", "P", "S"]
WIN_MSG = "You lost!"
LOSS_MSG = "You won!"
def decide_winner(user_choice, comp_choice):
  print "You took %s." % (user_choice)
  print "Computer selecting..."
  sleep(1)
  print "The computer took %s." % (comp_choice)
  user_choice_index = options.index(user_choice)
  comp_choice_index = options.index(comp_choice)
  if user_choice_index == comp_choice_index:
    print "Its a tie."
  elif user_choice_index == 0 and comp_choice_index == 2:
    print WIN_MSG
  elif user_choice_index == 1 and comp_choice_index == 0:
    print WIN_MSG
  elif user_choice_index == 2 and comp_choice_index == 1:
    print WIN_MSG
  elif user_choice_index > 2:
    print "You picked nonsense."
    return
  else:
    print LOSS_MSG
def play_RPS():
  print "Rock-Paper-Scissor"
  user_choice = raw_input("Select R for Rock, P for Paper, or S for Scissor ")
  sleep(1)
  user_choice = user_choice.upper()
  # comp_choice = options[randint(0,2)] hard coded, next lines contains soft code exists, just for remembering to not hardcode this code is still here
  comp_choice = options[randint(0,len(options)-1)]
  decide_winner(user_choice, comp_choice)
play_RPS()

My collaboration link:
https://www.codecademy.com/en/courses/python-ext/projects/rock-paper-scissors?user_id=57ab5bdc2ddfa3db4f00055f


#2

look here:

WIN_MSG = "You lost!"
LOSS_MSG = "You won!"

the variable which should hold the winning message holds the losing message, and vice versa....


#3

WOW, what an easy to spot mistake. Still: Thanks a lot for the fast answer.

I double checked of course whereever i used user_choice(index) and compchoice(_index), but did not think i´d make such a mistake.


#4

The mistakes which on paper at the easiest spot, are difficult in reality/practice

Because you think: I won't make such a silly mistake, so your brain prevents properly looking to it


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.