Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: collechess on August 04, 2011, 09:07:36 pm

Title: Rock Paper Scissors
Post by: collechess 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
Title: Re: Rock Paper Scissors
Post by: chattahippie 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?
Title: Re: Rock Paper Scissors
Post by: LincolnB 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"
Title: Re: Rock Paper Scissors
Post by: chattahippie 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
Title: Re: Rock Paper Scissors
Post by: Michael_Lee 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"
Title: Re: Rock Paper Scissors
Post by: calcdude84se 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.
Title: Re: Rock Paper Scissors
Post by: Ashbad 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.
Title: Re: Rock Paper Scissors
Post by: Munchor 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".
Title: Re: Rock Paper Scissors
Post by: calcdude84se 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.
Title: Re: Rock Paper Scissors
Post by: collechess on August 05, 2011, 12:30:50 pm
thank you all for your help
Title: Re: Rock Paper Scissors
Post by: Michael_Lee 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.
Title: Re: Rock Paper Scissors
Post by: Ashbad 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