What's the Purpose of LOSS_MESSAGE = "You lost!"?


#1

“”“this is a game of rocks, paper, scissors”""
from random import randint
from time import sleep

options = [‘R’, ‘P’, ‘S’]

LOSS_MESSAGE = 'You Lost!'
WIN_MESSAGE = ‘You Won!!!’

def decide_winner(user_choice, computer_choice):

print “You selected: %s” % user_choice
print 'Computer selecting…'
sleep(1)
print ‘Computer selected: %s’ % computer_choice

user_choice_index = options.index(user_choice)

computer_choice_index = options.index(computer_choice)

if user_choice_index == computer_choice_index:
print “It’s a Tie!”

elif user_choice_index == 0 and computer_choice_index == 2:
print ‘You Win!’

elif user_choice_index == 1 and computer_choice_index == 0:
print ‘You Win!!!’

elif user_choice_index == 2 and computer_choice_index == 1:
print ‘You Win!!!’

elif user_choice_index > 2:
print 'Wrong input’
return

else:
print ‘You Lose! Computer Wins!’

def play_RPS():
print 'Paper, Rock, and Scissors?'
user_choice = raw_input('Select R for Rock, P for Paper, or S for Scissors: ')
sleep(1)

user_choice = user_choice.upper()

computer_choice = options[randint(0,len(options)-1)]

decide_winner(user_choice,computer_choice)

play_RPS()

OK, I got this to run and all that with help from the hints. I’m now trying to go over it, trying to understand how it works together. I might be missing something but, what is the purpose of step 5 and step 6 where it says to code: LOSS_MESSAGE = “You lost!” and a WIN_MESSAGE = You Won! I don’t see those two variable called at all in any of the coding.


#2
print WIN_MESSAGE

The all caps indicates a CONSTANT. It is quite common to cache repeated phrases in a variable, rather than having the literals repeated throughout.


#3

Still don’t understand. Is it even called in the coding? I’m going thru it and don’t see print LOSS_MESSAGE.


#4
print LOSS_MESSAGE

Perhaps not repeated, but still much more visible to the reader.


#5

My mistake! I found my mistake. Sorry about that. Instead of using the LOSS_MESSAGE and WIN_MESSAGE, I actually put down print You Win! instead of using the LOSS_MESSAGE and WIN_MESSAGE. I understand now.

Ugh. Coding makes me feel as blind as a bat some times.


#6

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