Constantly getting the You lost! message printed to the console


#1

I have the program running. For some reason i keep having ‘Aww you lost!’ returning after me and the computer have made our choices.

I’m entering ‘rock’ for Rock, ‘paper’ for PAPER, and ‘scissors’ for SCISSORS.

I expected to have the You Won! message when i beat the computer but I still receive the lost message.


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 'User\'s choice is: %s' % user_choice
  print 'Computer\'s choice is: %s' % computer_choice
  if user_choice == computer_choice:
    print message['tie']
  elif user_choice == options.index('ROCK') and computer_choice == options.index('SCISSORS'):
    print message['won']
  elif user_choice == options.index('PAPER') and computer_choice == options.index('ROCK'):
    print message['won']
  elif user_choice == options.index('SCISSORS') and computer_choice == options.index('PAPER'):
    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()



#2

If none of the conditions evaluate to true you should probably take a look at what the values compared are


#3

Whats confusing me is why, when i choose rock and the computer chooses scissors, i still lose. i have an idea to try and ill post here if it fixes the issue.


#4

There’s nothing stopping you from printing out each side of the =='s
(compare what happens to what should happen)


#5

Thanks for your help. my problem was that I did options.index(‘rock’) instead of options[0] in my If statements.


#6

i thought the syntax was .index for lists and [0] for dicts


#7

Aside from their value literals ([] and {}), they don’t have any special syntax associated with them.
You can put .index or [0] after a variable referring to either of them. The dict would tell you it has no index attribute though, but the syntax is valid just the same. The [] operator (such as in a[1]) gets an item by key, something which they both support

…well not entirely true, there are list/dict comprehensions, pattern matching, function calls which do special things with lists and dicts, but it’s all unrelated to what you can do with a name that refers to a dict/list


#8

Yea I think I was just confused by what I can or cannot do. and Thank you. I’ll keep that in mind for dict having no index attribute.


#9

I’d either compare strings (no point involving your options list, no gain), or test whether the computer has the value that is one index higher (in your list, items to the right beat those to the left)

Better yet would be to forget about indexes entirely, they don’t help, just confusing. May as well simply say that rock beats scissors, and look at that when determining the winner.

who_beats_who = {
    'rock': 'scissors',
    ...
}

This also makes for rather easy extension of the program if adding more options:

who_beats_who = {
    'rock': [... all the things rock beats],
    ...
}

(Not that I suggest you change anything. The point of it is that the program could figure things out instead of hard-coding all the comparisons)


#10

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