0

I have a rock paper scissors code that works in python 2.7- it gives you 4 options (rock, paper scissors, or quit). However, I have to add a while loop to it and it somehow messes up my code whenever I add it. Can someone please help me? Thanks.

import sys
import random
import time
def main() :
    tie1 = "It's a draw- you both threw rock."
    tie2 = "It's a draw- you both threw paper."
    tie3 = "It's a draw- you both threw scissors."
    win1 = "You win! Your opponent threw scissors."
    win2 = "You win! Your opponent threw rock."
    win3 = "You win! Your opponent threw paper."
    lose1 = "You lost. Your opponent threw paper."
    lose2 = "You lost. Your opponent threw scissors."
    lose3 = "You lost. Your opponent threw rock."
    rand = random.randint(1,3)
    print "1)rock 2)paper 3)scissors 4)quit" #1 = rock 2 = paper 3 = scissors 4 = quit
    pick = raw_input ("What do you pick?: ").lower()
    if pick == "rock":
        if rand == (1):
            print (tie1)
            main()
        if rand == (2):
            print (lose1)
            main()
        if rand == (3):
            print (win1)
            main()
    if pick == "paper":
        if rand == (1):
            print (win1)
            main()
        if rand == (2):
            print (tie2)
            main()
        if rand == (3):
            print (lose2)
            main()
    if pick == "scissors":
        if rand == (1):
            print (lose3)
            main()
        if rand == (2):
            print (win3)
            main()
        if rand == (3):
            print (tie3)
            main()
    if pick == "quit":
        sys.exit (0)

main()        
5
Contributors
4
Replies
29
Views
9 Months
Discussion Span
Last Post by slate
0

Please show your altered code with the while() loop. One issue with your original code (which is what I assume you are showing here) is that it is infinitely recursive until "quit" is input. Eventually your stack will blow up. I suppose that is why you want a while() loop instead?

0

Something like this (just pseudocode). BTW I'm not sure this would be the recommended way of doing it ... Having a do/while loop that executes while true isn't ideal for newbies IMHO.

do
{
    "what do you pick?"
    >>

    if (pick == 'quit')
    {
        // We break out of the loop
        break;
    }

    // We're still in the loop, so let's see who won
    blah
    blah
    blah
}
while (true)
0

I know, you did not ask, but the program should be more tight (compact).

Something like this:

import random
def main() :
    tie_message="It's a draw. You both threw %s."
    win_message="You win! Your opponent threw %s."
    lose_message="You lost. Your opponent threw %s."
    values=("rock","paper","scissors") # must be in increasing beating order
    while True:
        print "1)rock 2)paper 3)scissors 4)quit" 
        pick = raw_input ("What do you pick?: ").lower()
        rand = random.choice(values)
        if pick == "quit":
            break
        if pick not in values:
            print "Invalid choice!"
            continue
        if pick == rand:
            print tie_message % rand
        else:
            pick_index=values.index(pick)
            rand_index=values.index(rand) 
            if (pick_index,rand_index) in ((0,1),(1,2),(2,0)):
                print lose_message % rand
            else:
                print win_message % rand
main()

Edited by slate

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.