1

I'm very new to Python but have written a simple Rock, Paper, Scissors game.

It's code is as follows:

from __future__ import print_function
import random

name = raw_input("Hi, I'm PyVa.  What is your name: ")
print('Hi',name, end='.')
yesorno = raw_input('  Would you like to play Rock, Paper, Scissors?: ')
yesorno = str(yesorno)



if yesorno == str('Yes'):
    choices = ['Rock', 'Paper', 'Scissors']
    print('Ok')
    your_choice = raw_input('Choose Rock, Paper or Scissors: ')
    print('My turn...')

    my_choice = random.choice(choices)
    print('I choose,', my_choice)
    if your_choice == my_choice:
        print('We both choose the same, it is a draw.')
    elif your_choice == 'Rock' and my_choice == 'Paper':
        print('I win!')
    elif your_choice == 'Scissors' and my_choice == 'Paper':
        print('You win!')
    elif your_choice == 'Paper' and my_choice == 'Rock':
        print('You win!')
    elif your_choice == 'Paper' and my_choice == 'Scissors':
        print('I win!')
    elif your_choice == 'Rock' and my_choice == 'Scissors':
        print('You win!')
    elif your_choice == 'Scissors' and my_choice == 'Rock':
        print('I win!')

    again = raw_input('Would you like to play again?:')

    #this is where I would like to loop to the start depending on input.



else:
    print('Ok, maybe we can play later, bye.')

Now I can imagine this isn't even remotely elegant code and that there must be more precise ways to write what I am trying to do. (Please give any pointers you have time to).

My main concern is how to correctly insert a loop for after each game ends to either return to the second block ie. the start or the game, or to simply end, based on the raw_input from the user.

Many thanks for any help you can give.

2
  • You don't need str('Yes'). 'Yes' is already a string. The same applies to the return value of raw_input. Commented Jun 1, 2015 at 16:44
  • Thank you all for your answers, my rep doesn't allow me to upvote or I would have. Commented Jun 2, 2015 at 18:47

5 Answers 5

2

There you go:

from __future__ import print_function
import random

name = raw_input("Hi, I'm PyVa.  What is your name: ")
print('Hi',name, end='.')
n = 0
while True:
    if 'y' in raw_input('Would you like to play Rock, Paper, Scissors?: ').lower() or n > 0:
        my_choice = random.choice(['rock', 'paper', 'scissors'])
        print('Ok')
        your_choice = raw_input('Choose Rock, Paper or Scissors: ').lower()
        print('My turn...')
        print('I choose,', my_choice)
        if your_choice == my_choice:
            print('We both choose the same, it is a draw.')
        elif your_choice in ['rock', 'scissors'] and my_choice == 'paper':
            print('I win!')
        elif your_choice == 'paper' and my_choice in ['rock', 'scissors']:
            print('You win!')
        elif your_choice == 'rock' and my_choice == 'scissors':
            print('You win!')
        elif your_choice == 'scissors' and my_choice == 'rock':
            print('I win!')
        n += 1
        if not 'y' in raw_input('Would you like to play again?:').lower():
            break

    #this is where I would like to loop to the start depending on input.

It's also case insensitive now, kind of annoying when the game exits or you lose because you accidentally forgot to write your answer starting with a capital letter. I also simplified it a little bit using 'in'

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for taking the time to provide working code for me. Based on my limited beginner knowledge I added a while loop based on another answer, and used else to check for a no, looping back on a yes. I'm going to study your changes so that I understand them and can incorporate the theory in any future instances where required.
You're welcome! If you have any questions do not hesitate to contact me. I'm sure you'll write very nice code in no time!
0

I would insert the loop there:

# ...

yesorno = raw_input('  Would you like to play Rock, Paper, Scissors?: ')

while yesorno == 'Yes':
    choices = ['Rock', 'Paper', 'Scissors']

    # ...

    yesorno = raw_input('  Would you like to play Rock, Paper, Scissors?: ')

print('Ok, maybe we can play later, bye.')

Comments

0
while True:

    <your code>

    if not 'y' in raw_input('Would you like to play again?: ').lower():
        break;

2 Comments

While this answer is correct and uses the same logic as my answer, I strongly recomend taking a look at my answer if you want to avoid having the initial question asked multiple times. The short is also shorter among some other things.
I assume he will add the initial print outside the fragment, not mentioned, but seems quite trivial to me.
0

What I'd do is to remove the unnecessary variables and instead put the actual game in a function. This allows you to call it once WITHOUT asking if the player wants to replay, and then make a while loop where the user can decide if he wants to play on. I hope this helps. I couldn't test this myself as I use Python 2.7, but aside from some potential syntax differences the code should work. I suggest something like this:

from __future__ import print_function
import random

name = raw_input("Hi, I'm PyVa.  What is your name: ")
print('Hi',name, end='.')
yesorno = raw_input('  Would you like to play Rock, Paper, Scissors?: ')
if yesorno == 'y':
    play()
def play():
        choices = ['Rock', 'Paper', 'Scissors']
        print('Ok')
        your_choice = raw_input('Choose Rock, Paper or Scissors: ')
        print('My turn...')

    my_choice = random.choice(choices)
    print('I choose,', my_choice)
    if your_choice == my_choice:
        print('We both choose the same, it is a draw.')
    elif your_choice == 'Rock' and my_choice == 'Paper':
        print('I win!')
    elif your_choice == 'Scissors' and my_choice == 'Paper':
        print('You win!')
    elif your_choice == 'Paper' and my_choice == 'Rock':
        print('You win!')
    elif your_choice == 'Paper' and my_choice == 'Scissors':
        print('I win!')
    elif your_choice == 'Rock' and my_choice == 'Scissors':
        print('You win!')
    elif your_choice == 'Scissors' and my_choice == 'Rock':
        print('I win!')
while True:
    play_again = raw_input('Play Again?(Y/N)')
    if play_again.lower() == 'y':
        play()
    elif play_again.lower() == 'n':
        print('Ok, maybe we can play later, bye.')
        break
    else:
        print('That isn\'t a valid option...')

Comments

-1

I don't have time to clean it up, but here's a way you can fix your specific issue. You wrap the game within a while loop that is always true, and exit when desired.

from __future__ import print_function
import random

name = raw_input("Hi, I'm PyVa.  What is your name: ")
print('Hi',name, end='.')

yesorno = raw_input('  Would you like to play Rock, Paper, Scissors?: ')

while(1):

    yesorno = str(yesorno)

    if yesorno == str('Yes'):
        choices = ['Rock', 'Paper', 'Scissors']
        print('Ok')
        your_choice = raw_input('Choose Rock, Paper or Scissors: ')
        print('My turn...')

        my_choice = random.choice(choices)
        print('I choose,', my_choice)
        if your_choice == my_choice:
            print('We both choose the same, it is a draw.')
        elif your_choice == 'Rock' and my_choice == 'Paper':
            print('I win!')
        elif your_choice == 'Scissors' and my_choice == 'Paper':
            print('You win!')
        elif your_choice == 'Paper' and my_choice == 'Rock':
            print('You win!')
        elif your_choice == 'Paper' and my_choice == 'Scissors':
            print('I win!')
        elif your_choice == 'Rock' and my_choice == 'Scissors':
            print('You win!')
        elif your_choice == 'Scissors' and my_choice == 'Rock':
            print('I win!')

        again = raw_input('Would you like to play again? [y/n]:')

        if again is "y":
            next
        else:
            print('Thanks for playing!\n')
            exit()

    else:
        print('Ok, maybe we can play later, bye.')

2 Comments

Thank you, I used your solution. It made the most sense to me.
I had to play around with the loop a bit and the indentations. In the end I simply changed yerorno variable to one I could use uniformally in the code and added an else statement in case of n.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.