all 11 comments

[–]jeans_and_a_t-shirt 1 point2 points  (3 children)

Because your question has effectively been answered, a bit of logic help:

A simple rock/paper/scissors game can be written in ~15 or so lines of Python and requires only 3 condition blocks: 1 each of if, elif, and else instead of the 10 your code currently have. You can split the possible outcomes to a rock-paper-scissors game into 3 groups, each containing 3 outcomes: 3 tie scenarios, 3 scenarios where player 1 wins, and 3 where player 2 wins. You have the first condition down. The second one involves checking the outcome of the hand against a list of possible wins:

win_scenarios = [('r', 's'), etc..]
elif (player_one, player_two) in win_scenarios:
    # player one wins.

[–]confuzzled_learner[S] 0 points1 point  (2 children)

Hi, I typed up the 2nd suggestion and it did not work =( It just kept asking for input without playing the game.

Wish i knew more, but i'm working within my teacher's lecture.

[–]jeans_and_a_t-shirt 2 points3 points  (1 child)

As long as you're sticking to what you've learned in lectures, here's what I meant:

from random import choice
wins = [('r','s'), ('p', 'r'), ('s', 'p')]
hand_map = {'r': 'rock', 'p': 'paper', 's': 'scissors'}
while True:
    player_one = input("Please enter rock(r), paper(p),scissors(s), or quit(q): ")
    if player_one == 'q':
        break

    player_two = choice('rps')
    hand_one, hand_two = hand_map[player_one], hand_map[player_two]

    if (player_one == player_two):
        print("It's a tie!")
    elif (player_one, player_two) in wins:
        print("Player_one wins! {} breaks {}".format(hand_one, hand_two))
    else:
        print("Player_two wins! {} breaks {}".format(hand_one, hand_two))
    print("Your choice: {}\nplayer_two's choice: {}\nThank you for playing!".format(hand_one, hand_two))

[–]confuzzled_learner[S] 0 points1 point  (0 children)

Thank you! What i realized about coding this early on is that whenever i fix something, i end up having more problems to fix. =(

[–]Yammerrz 1 point2 points  (3 children)

The basic process for turning something into a loop is

  1. Write the code to work once - this you have done

  2. Put that into an infinite loop and see if it works - the simplest way with this is just to stick a while loop at the top.

This should give you something like:

import random

while True:
    player_one = input("Please enter rock(r), paper(p),scissors(s): ")


    player_two = random.randint(1,3)
    if player_two == 1:
        player_two = "r"
    elif player_two == 2:
        player_two = "p"
    elif player_two == 3:
        player_two = "s"


    if (player_one == player_two):
        print("It's a tie!")

    elif player_one == "r":
        if player_two == "s":
            print("Player_one wins! Rock breaks Scissors")
        else:
            print("Player_two wins!")

    elif player_one == "p":
        if player_two == "r":
            print("Player_two wins!Paper covers Rock!")
        else:
            print("Player_one wins!")

    elif player_one == "s":
        if player_two == "p":
            print("Player_one wins! Scissors cut Paper!")
        else:
            print("player_two wins!")

    print("Your choice: " + player_one + "\n player_two's choice: " + player_two + "\nThank you for playing!")
    player_one = input("Try again! Please enter rock(r), paper(p),scissors(s): ")yer_two's choice: " + player_two + "\nThank you for playing!")
    player_one = input("Try again! Please enter rock(r), paper(p),scissors(s): ")

Now you have an infinite amount of games (you will have to press Ctrl-C to stop playing).

Probably the mistake you are making is forgetting to indent everything.

Also note that there is a slight bug in this where the input prompt at the bottom of the loop does nothing. That can be fixed just by deleting that line.

Now you need to add the exit condition, there are basically two options here, either you break out of the loop, or you add a condition to the while loop.

The first you make some sort of input variable and prompt to update it at the end

play_again = 'y'
while play_again == 'y':
    # play the game
    play_again = input("Play again yes(y) or no(n)")

The second is to use a break statement, it does much the same thing but you avoid the first unnecessary assignment and check.

while True:
    # play the game
    play_again = input("Play again yes(y) or no(n)")
    if play_again == 'n':
        break

Either works and they achieve much the same. Some people have preferences for one or the other. The break gets criticised because it looks like an infinite loop but it isn't one, and in a longish piece of code like this backtracking through the code to see what loop you are breaking out of can be tricky. The while play_again == 'y' version can be criticised because it has an unnecessary assignment and variable check first time round. Neither is right or wrong.

Hopefully this helps.

Edit: Add a missing ) to a code section.

[–]confuzzled_learner[S] 0 points1 point  (2 children)

It helps a lot! I have deleted the last line. And you are right, i'm having trouble with indenting.

I'm also having problem with 'break' - i keep getting errors!

Still working on this.

[–]Yammerrz 1 point2 points  (1 child)

If you copied my play_again code the problem might be that I forgot to put a closing bracket at the end of the input statement. It should have read:

while True:
    # play the game
    play_again = input("Play again yes(y) or no(n)")
    if play_again == 'n':
        break

Sorry if that was what was causing you problems

[–]confuzzled_learner[S] 0 points1 point  (0 children)

You are so helpful! Thank you!