Author Topic: Rock Paper Scissors  (Read 4312 times)

0 Members and 1 Guest are viewing this topic.

Offline collechess

  • LV3 Member (Next: 100)
  • ***
  • Posts: 93
  • Rating: +22/-2
    • View Profile
Rock Paper Scissors
« on: August 04, 2011, 09:07:36 pm »
So I just started learning python,(like 3 hours ago :)),and I created a text based rock, paper, scissors program.  However, I cannot get it to allow the player to quit.  Here is the code.
Code: [Select]
import random
while True:
    playerchoice=0
    compchoice=random.randrange(1,3)
    outcome=0
    quit="quit"
    rock="rock"
    paper="paper"
    scissors="scissors"
    while playerchoice==0:
        playerpick=raw_input("choice:")

        if playerpick==quit:
            break
            break
            break
         
        if playerpick==rock:
            playerchoice=1

        elif playerpick==paper:
            playerchoice=2

        elif playerpick==scissors:
            playerchoice=3

    if compchoice==1:
        print "vs rock"

    if compchoice==2:
        print "vs paper"

    if compchoice==3:
        print "vs scissors"

    if playerchoice==compchoice:
        print "tie"

    if playerchoice==1 and compchoice==2:
        outcome=2

    if compchoice==1 and playerchoice==2:
        outcome=1

    if playerchoice==1 and compchoice==3:
        outcome=1

    if compchoice==1 and playerchoice==3:
        outcome=2

    if playerchoice==2 and compchoice==3:
        outcome=2

    if compchoice==2 and playerchoice==3:
        outcome=1
       
    if outcome==1:
        print "u win"

    elif outcome==2:
        print "u lose"

Thanks

Offline chattahippie

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +27/-0
  • Super Member! :D
    • View Profile
Re: Rock Paper Scissors
« Reply #1 on: August 04, 2011, 10:50:26 pm »
I myself don't know Python, but is there a jump-like command, so you can jump to outside of the While True: loop?

Offline LincolnB

  • Check It Out Now
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1115
  • Rating: +125/-4
  • By Hackers For Hackers
    • View Profile
Re: Rock Paper Scissors
« Reply #2 on: August 04, 2011, 11:03:26 pm »
Now, I don't know Python very well, I'm familiar with the syntax but haven't really done anything with it. However, I do think it would be possible, instead of saying 'While 1' or 'While True', a condition that loops infinitely, set up a boolean variable (or any kind) called Game_Is_Running, set it initially to true, or 1, or whatever, and you could say something like 'While Game_Is_Running == 1 or true or whatev. And then, where you say; if playerpick==quit: Game_Is_Running = 2 or someting like that, so the next iteration of the loop it would check if the variable is 1, and if it's not, terminate.

Some Code: (sorry I didn't put it in the BBcode "[ code ]" tag things I couldn't get bold to work with that )


import random
game_running = 1
While game_running==1:

    playerchoice=0
    compchoice=random.randrange(1,3)
    outcome=0
    quit="quit"
    rock="rock"
    paper="paper"
    scissors="scissors"
    while playerchoice==0:
        playerpick=raw_input("choice:")

        if playerpick==quit:
            game_running = 2
        
        if playerpick==rock:
            playerchoice=1

        elif playerpick==paper:
            playerchoice=2

        elif playerpick==scissors:
            playerchoice=3

    if compchoice==1:
        print "vs rock"

    if compchoice==2:
        print "vs paper"

    if compchoice==3:
        print "vs scissors"

    if playerchoice==compchoice:
        print "tie"

    if playerchoice==1 and compchoice==2:
        outcome=2

    if compchoice==1 and playerchoice==2:
        outcome=1

    if playerchoice==1 and compchoice==3:
        outcome=1

    if compchoice==1 and playerchoice==3:
        outcome=2

    if playerchoice==2 and compchoice==3:
        outcome=2

    if compchoice==2 and playerchoice==3:
        outcome=1
        
    if outcome==1:
        print "u win"

    elif outcome==2:
        print "u lose"
« Last Edit: August 04, 2011, 11:05:36 pm by buttsfredkin »
Completed Projects:
   >> Spacky Emprise   >> Spacky 2 - Beta   >> Fantastic Sam
   >> An Exercise In Futility   >> GeoCore

My Current Projects:

Projects in Development:
In Medias Res - Contest Entry

Talk to me if you need help with Axe coding.


Spoiler For Bragging Rights:
Not much yet, hopefully this section will grow soon with time (and more contests)



Offline chattahippie

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +27/-0
  • Super Member! :D
    • View Profile
Re: Rock Paper Scissors
« Reply #3 on: August 04, 2011, 11:13:21 pm »
You could use a massive return( function to display if you win or lose, it would optimize this a lot, but would return True or False :(
I just went on CodingBat, which has a lot of nice Python practices (that's where I learned that ;))

EDIT: that wouldn't work, I don't think it displays it, only outputs data, but that might still be nice for other parts of the program
« Last Edit: August 04, 2011, 11:19:09 pm by chattahippie »

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Rock Paper Scissors
« Reply #4 on: August 05, 2011, 01:30:45 am »
So, a few quick tips: adding three 'breaks' in a row won't make your program jump out of three loops -- the interpreter will see the first break, and will jump out of your while loop and ignore all the other 'breaks'. 

Instead, either restructure your program so that it'll change a variable that'll let you quit, or import the module 'sys' and do 'sys.exit()'.

Also, lists in Python are awesomely useful.  In this particular case, I was able to use lists to optimize away several 'if' statements.

Here's slightly more optimized version that I wrote:

Code: [Select]
import sys
import random

# A list containing all your choices.
choices = ["rock", "paper", "scissors"]

# I added an underscore because I think 'quit' is a keyword in python.
quit_ = "quit"

while True:
    playerchoice = 0
    playerpick = ""

    # The computer will pick a number from zero up to (but not including) three.
    compchoice = random.randrange(0,3)

    # This way, it'll keep looping until you enter a valid input.
    while playerpick not in choices:
        playerpick = raw_input("choice:")

        if playerpick == quit_:
            # sys.exit() forces the program to quit.
            sys.exit()

    # Searches the list 'choices' and returns which position the string was in.
    playerchoice = choices.index(playerpick)

    # Optimization.
    print "vs " + choices[compchoice]

    if compchoice == playerchoice:
        print "tie"
    elif (compchoice + 1) % 3 == playerchoice:
        # Also an optimization.
        print "u win"
    else:
        print "u lose"
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Rock Paper Scissors
« Reply #5 on: August 05, 2011, 08:13:28 am »
Try to avoid sys.exit, since it makes it difficult to embed your programs. Still a better version:
Code: [Select]
# A list containing all your choices.
choices = ["rock", "paper", "scissors", "quit"] #make "quit" another choice

while 1: #more idiomatic, and very slightly faster than "while True:"
    #No need to set either player* variable beforehand

    # The computer will pick a number from zero up to (but not including) three.
    compchoice = random.randrange(0,3)

    # This way, it'll keep looping until you enter a valid input.
    while playerpick not in choices:
        playerpick = raw_input("choice: ") #A stylistic choice, but I like spaces after my colons

    # Searches the list 'choices' and returns which position the string was in.
    playerchoice = choices.index(playerpick)

    #Quit if the player picked 3 ("quit")
    if playerchoice == 3:
        break #This successfully leaves the main loop

    # Optimization.
    print "vs " + choices[compchoice]

    if compchoice == playerchoice:
        print "tie"
    elif (compchoice + 1) % 3 == playerchoice:
        # Also an optimization.
        print "u win"
    else:
        print "u lose"

Edit: optionally, you can set playerpick to 'raw_input("choice: ").lower()', which allows use of both upper-, lower-, and mixed-case for input.
« Last Edit: August 05, 2011, 08:15:08 am by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Ashbad

  • Guest
Re: Rock Paper Scissors
« Reply #6 on: August 05, 2011, 08:15:42 am »
even better version:

Code: [Select]
import rock_paper_scissors

play()

:)

More seriously, good luck, but I personally can't help much.
« Last Edit: August 05, 2011, 12:41:50 pm by Ashbad »

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Rock Paper Scissors
« Reply #7 on: August 05, 2011, 09:13:04 am »
Michael Lee, I suggest using random.choice() to choose randomly from a list. With calc84maniac's implementation this can't be done though because the bot could also choose "list".

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Rock Paper Scissors
« Reply #8 on: August 05, 2011, 09:33:32 am »
Michael Lee, I suggest using random.choice() to choose randomly from a list. With calc84maniac's implementation this can't be done though because the bot could also choose "list".
*calcdude84se
There's nothing wrong with indexing AFAIK. In fact, you want an index because it makes determining win/lose easier.
« Last Edit: August 05, 2011, 12:47:08 pm by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline collechess

  • LV3 Member (Next: 100)
  • ***
  • Posts: 93
  • Rating: +22/-2
    • View Profile
Re: Rock Paper Scissors
« Reply #9 on: August 05, 2011, 12:30:50 pm »
thank you all for your help

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Rock Paper Scissors
« Reply #10 on: August 05, 2011, 01:04:12 pm »
even better version:

Code: [Select]
import rock_paper_scissors

play()
...is it sad that I actually tried this, and honestly believed it would work?


Code: [Select]
while 1: #more idiomatic, and very slightly faster than "while True:"
Huh, I didn't know that.  It seems that's the case only for Python 2.x though -- in Python 3.x, there's no difference between the two, apparently.
« Last Edit: August 05, 2011, 01:04:43 pm by Michael_Lee »
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Ashbad

  • Guest
Re: Rock Paper Scissors
« Reply #11 on: August 05, 2011, 01:51:45 pm »
even better version:

Code: [Select]
import rock_paper_scissors

play()
...is it sad that I actually tried this, and honestly believed it would work?


Code: [Select]
while 1: #more idiomatic, and very slightly faster than "while True:"
Huh, I didn't know that.  It seems that's the case only for Python 2.x though -- in Python 3.x, there's no difference between the two, apparently.

Wait, you mean it didn't work?  :(  sad day for mankind