9 Replies - 1231 Views - Last Post: 25 September 2015 - 08:18 AM Rate Topic: -----

#1 flintdroe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-September 15

Made Rock Paper Scissors and while loop wont work

Posted 24 September 2015 - 11:01 AM

import random
def game():
    choices = ['Rock', 'Paper', 'Scissors']
    
    
    print 'Are you ready to play?'
    print 'Your choices are Rock, Paper, Scissors'
    user = raw_input()
    user.upper()
    comp = random.choice(choices)
    
    if user == 'Rock':
        print 'You have chosen Rock'
        if comp == 'Rock':
            print 'You both have chosen Rock, you Tie'
        elif comp =='Paper':
            print 'Paper covers rock, and you just lost to a machine'
        elif comp == 'Scissors':
            print 'Rock beats paper, and maybe we can win the robot revolution'
        else:
            print 'Does not compute'
    
    
    if user == 'Paper':
        print 'You have chosen Paper'
        if comp == 'Rock':
            print 'Paper covers rock, WINNING'
        elif comp =='Paper':
            print 'IT IS A TIE!!!!'
        elif comp == 'Scissors':
            print 'You just lost to a computer. That is truly dissapointing'
        else:
            print 'Does not compute'
    
    
    if user == 'Scissors':
        print 'You have chosen Scissors'
        if comp == 'Rock':
            print 'That is unfortunate'
        elif comp =='Paper':
            print 'Snip snip snip, there goes the paper. You win'
        elif comp == 'Scissors':
            print 'Please stop swordfighting. You tie'
        else:
            print 'Does not compute'
    
playAgain = True

def main():
    while playAgain == True:
        game()
        
  
        print 'Play Again? (Y or N)'
        x = raw_input()
        
        x = x.upper()
        if x == 'Y':
            playAgain = True
        else:
            playAgain = False
     
    
main()
    


Is This A Good Question/Topic? 0
  • +

Replies To: Made Rock Paper Scissors and while loop wont work

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14045
  • View blog
  • Posts: 56,217
  • Joined: 12-June 08

Re: Made Rock Paper Scissors and while loop wont work

Posted 24 September 2015 - 11:08 AM

Quote

while loop wont work

Please elaborate.. won't work.. _how_?
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: Made Rock Paper Scissors and while loop wont work

Posted 24 September 2015 - 03:36 PM

In your code, main() expects playAgain to be a local variable while you want it to be considered as a global variable. To do this, add global playAgain inside main() before using it.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6546
  • View blog
  • Posts: 26,533
  • Joined: 12-December 12

Re: Made Rock Paper Scissors and while loop wont work

Posted 24 September 2015 - 04:38 PM

Note also that
    user.upper()
    comp = random.choice(choices)
    
    if user == 'Rock':

you are converting their choice to uppercase but still comparing it to title case.

View Postborn2c0de, on 24 September 2015 - 10:36 PM, said:

In your code, main() expects playAgain to be a local variable while you want it to be considered as a global variable. To do this, add global playAgain inside main() before using it.

.. or move the assignment of playAgain (line 47) into main().
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6546
  • View blog
  • Posts: 26,533
  • Joined: 12-December 12

Re: Made Rock Paper Scissors and while loop wont work

Posted 24 September 2015 - 04:46 PM

Out of interest, it occurred to me to use tuples as dictionary keys to significantly reduce the code:
results = {('ROCK', 'ROCK') : 'draw', ('ROCK', 'SCISSORS') : 'user wins'}
key = 'ROCK', 'SCISSORS'
print(results[key])

This is just out of interest though; there is nothing wrong with your code, it is perfectly clear (particularly if you haven't covered tuples or dicts yet).

This post has been edited by andrewsw: 24 September 2015 - 04:47 PM

Was This Post Helpful? 1
  • +
  • -

#6 flintdroe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-September 15

Re: Made Rock Paper Scissors and while loop wont work

Posted 25 September 2015 - 06:25 AM

View Postmodi123_1, on 24 September 2015 - 11:08 AM, said:

Quote

while loop wont work

Please elaborate.. won't work.. _how_?


It wont loop the game so I can play it more than once

View Postandrewsw, on 24 September 2015 - 04:46 PM, said:

Out of interest, it occurred to me to use tuples as dictionary keys to significantly reduce the code:
results = {('ROCK', 'ROCK') : 'draw', ('ROCK', 'SCISSORS') : 'user wins'}
key = 'ROCK', 'SCISSORS'
print(results[key])

This is just out of interest though; there is nothing wrong with your code, it is perfectly clear (particularly if you haven't covered tuples or dicts yet).

Thanks for the reply! and we have not covered tuples or dicts yet, but I see how that would make the code a lot simpler.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6546
  • View blog
  • Posts: 26,533
  • Joined: 12-December 12

Re: Made Rock Paper Scissors and while loop wont work

Posted 25 September 2015 - 06:45 AM

Have you made the corrections already mentioned? In which case, post your updated code. (Your posted code won't run, giving an error:

UnboundLocalError: local variable 'playAgain' referenced before assignment.)
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6546
  • View blog
  • Posts: 26,533
  • Joined: 12-December 12

Re: Made Rock Paper Scissors and while loop wont work

Posted 25 September 2015 - 06:53 AM

Actually, you haven't converted to uppercase.
    user.upper() # this doesn't do anything, the result is just discarded
    # should be
    user = user.upper()

Was This Post Helpful? 0
  • +
  • -

#9 flintdroe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-September 15

Re: Made Rock Paper Scissors and while loop wont work

Posted 25 September 2015 - 07:46 AM

This code is now working
import random
def game():
    choices = ['Rock', 'Paper', 'Scissors']
    
    
    print 'Are you ready to play?'
    print 'Your choices are Rock, Paper, Scissors'
    user = raw_input()
    user.upper()
    comp = random.choice(choices)
    
    if user == 'Rock':
        print 'You have chosen Rock'
        if comp == 'Rock':
            print 'You both have chosen Rock, you Tie'
        elif comp =='Paper':
            print 'Paper covers rock, and you just lost to a machine'
        elif comp == 'Scissors':
            print 'Rock beats paper, and maybe we can win the robot revolution'
        else:
            print 'Does not compute'
    
    
    if user == 'Paper':
        print 'You have chosen Paper'
        if comp == 'Rock':
            print 'Paper covers rock, WINNING'
        elif comp =='Paper':
            print 'IT IS A TIE!!!!'
        elif comp == 'Scissors':
            print 'You just lost to a computer. That is truly dissapointing'
        else:
            print 'Does not compute'
    
    
    if user == 'Scissors':
        print 'You have chosen Scissors'
        if comp == 'Rock':
            print 'That is unfortunate'
        elif comp =='Paper':
            print 'Snip snip snip, there goes the paper. You win'
        elif comp == 'Scissors':
            print 'Please stop swordfighting. You tie'
        else:
            print 'Does not compute'
    
playAgain = True

def main():
    while playAgain == True:
        game()
        
  
        print 'Play Again? (Y or N)'
        x = raw_input()
        
        x = x.upper()
        if x == 'Y':
            playAgain = True
        else:
            playAgain = False
            
        global playAgain
        

    
main()

Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6546
  • View blog
  • Posts: 26,533
  • Joined: 12-December 12

Re: Made Rock Paper Scissors and while loop wont work

Posted 25 September 2015 - 08:18 AM

You should put the global statement just inside the function:
    def main():
        global playAgain

It is confusing and error-prone otherwise.

Personally, as I mentioned, I would move the assignment of playAgain inside main() to avoid the global, it isn't needed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1