Code not working like I want it to


#1



https://www.codecademy.com/en/courses/python-ext/projects/rock-paper-scissors


the program works fine altogether but in the end it declares 'I am clueless here' (which it shouldnt) because all conditions written before are true..


I expect it shouldnt declare 'i am clueless here' as I have written all conditions.


"""In this program, you will play ROCK, PAPER AND SCISSORS with our very own computer. Lets see if you can win this"""


from random import choice
from time import sleep

def prompt_user():
    user_choice= str(raw_input('Please choose from Rock, Paper or Scissors; You must choose only one '))
    user_choice= user_choice.upper()
    if user_choice == 'ROCK' or user_choice=='PAPER' or user_choice== 'SCISSORS':
        print 'you choose ' + str(user_choice)
        return user_choice
    else:
        return prompt_user()



def computer_action():
    computer_choice= ['ROCK', 'PAPER', 'SCISSORS']
    computer_choice = choice(computer_choice)
    
    print 'computer is thinking'
    sleep(3)
   
    print 'computer chose %s '% (computer_choice)
    return computer_choice
  
     

guess_1= prompt_user()    
guess_2= computer_action()
    
if guess_1 == 'ROCK' and guess_2== 'ROCK':
    print ' its a damn tie'
if guess_1== 'PAPER' and guess_2== 'PAPER':
    print 'its a damn tie'
if guess_1== 'SCISSORS' and guess_2== 'SCISSORS':
    print 'its a damn tie'  
if guess_1== 'PAPER' and guess_2== 'ROCK':
    print 'You won buddy..'
if guess_1== 'SCISSORS' and guess_2== 'PAPER':
    print 'You won. way to go'
if guess_1== 'ROCK' and guess_2=='SCISSORS':
    print 'You won'
if guess_1== 'PAPER' and guess_2== 'SCISSORS':
    print 'You lost'
if guess_1== 'ROCK' and guess_2== 'PAPER':
    print 'You lost'
if guess_1== 'SCISSORS' and guess_2=='ROCK':
    print 'you lost'
else:
    print 'I am clueless here...'


#2

here:

if guess_1== 'SCISSORS' and guess_2=='ROCK':
    print 'you lost'
else:
    print 'I am clueless here...

only if guess_1 is scissors and guess_2 is rock, will "i am clueluess here" not run

what you want to do requires, if then elif (a lot ) and then finally else


#3

I see.. Thanks a lot.
by the way, is there any cleaner,shorter way to write the code consisting of if, elif statements??


#4

well, some languages have switch statements, but python doesn't, so, no there isn't

I would personally use a completely different approach to solve RPS to make the code shorter


from random import randint
options = ["rock", "paper", "scissors"]
while True:
    x = raw_input("Choice rock, paper or scissors: ").lower()
    if x in options:
         userChoice = options.index(x)
         break
    else:
         print "invalid choice, try again"

computerChoice = randint(0,2)
print "the computer picked: {}".format(options[computerChoice])

def compare(choice1, choice2):
    if choice1 == choice2:
        return "The result is a tie!"
    elif  (choice1 + 1) % 3 == choice2:
         return "You lost"
    else:
         return "You won"

print compare(userChoice,computerChoice)

`


#5

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