How to repeat game of rock, paper, scissors in python

Question:

I have made a rock, paper, scissors game in python. How would I get the game to repeat over if the user enters yes? My code appears to go into a never ending loop when the user enters anything but rock, paper, or scissors

Also I’m trying to learn when and where I should use functions. If you could show a pythonic way to separate the finished code into functions I would greatly appreciate that.

import random

a = ["rock", "paper", "scissors"]

word = input('Enter rock, paper, or scissors: ')

def game():
    while True:
        try:
            if word not in a:
                raise ValueError #this will send it to the print message and back to the input option
            break
        except ValueError:
            print(" You must enter rock, paper, or scissors.")


    comp_draw = random.choice(a)
    print('The computer drew ' + comp_draw)

    if comp_draw == 'rock' and word=='rock':
        print('It was a tie')
    elif comp_draw == 'paper' and word=='paper':
        print('It was a tie')
    elif comp_draw == 'scissors' and word=='scissors':
        print('It was a tie')

    elif comp_draw == 'paper' and word=='rock':
        print('you lost')
    elif comp_draw == 'rock' and word=='paper':
        print('you won!')

    elif comp_draw == 'rock' and word=='scissors':
        print('you lost')
    elif comp_draw == 'scissors' and word=='rock':
        print('you won!')

    elif comp_draw == 'scissors' and word=='rock':
        print('you won!')
    elif comp_draw == 'scissors' and word=='rock':
        print('you won!')

game()


play_again = input('would you like to play again: ')
Asked By: Nick T

||

Answers:

Wasn’t missing much, all you needed was a loop

import random

a = ["rock", "paper", "scissors"]

word = input('Enter rock, paper, or scissors: ')

def game():
    while True:
        try:
            if word not in a:
                raise ValueError #this will send it to the print message and back to the input option
            break
        except ValueError:
            print(" You must enter rock, paper, or scissors.")


    comp_draw = random.choice(a)
    print('The computer drew ' + comp_draw)

    if comp_draw == 'rock' and word=='rock':
        print('It was a tie')
    elif comp_draw == 'paper' and word=='paper':
        print('It was a tie')
    elif comp_draw == 'scissors' and word=='scissors':
        print('It was a tie')

    elif comp_draw == 'paper' and word=='rock':
        print('you lost')
    elif comp_draw == 'rock' and word=='paper':
        print('you won!')

    elif comp_draw == 'rock' and word=='scissors':
        print('you lost')
    elif comp_draw == 'scissors' and word=='rock':
        print('you won!')

    elif comp_draw == 'scissors' and word=='rock':
        print('you won!')
    elif comp_draw == 'scissors' and word=='rock':
        print('you won!')


play_again = "yes"

while play_again == "yes":

    game()

    play_again = input('would you like to play again: ').lower()
Answered By: Xtrem532

Instead of :

game() 
play_again = input("...")

Put your game in a loop as follow :

while True:
    play_again = input ("would you like to play")
    if play_again == "yes" :
        game() 
        continue 
    elif play_again == "no" :
        break
    else:
        print("answer by yes or no") 
        continue 
Answered By: moe asal

There was also an issue of the variable word being outside the scope of game(). It would work on the first run but not the consequent runs thereafter. This code seems to work fine and as expected.

import random

a = ["rock", "paper", "scissors"]

def game():
    while True:
        try:
            word = input('Enter rock, paper, or scissors: ')
            if word not in a:
                raise ValueError # this will send it to the print message and back to the input option
            break
        except ValueError:
            print(" You must enter rock, paper, or scissors.")


    comp_draw = random.choice(a)
    print('The computer drew ' + comp_draw)

    if comp_draw == 'rock' and word =='rock':
        print('It was a tie')
    elif comp_draw == 'paper' and word =='paper':
        print('It was a tie')
    elif comp_draw == 'scissors' and word =='scissors':
        print('It was a tie')
    elif comp_draw == 'paper' and word =='rock':
        print('you lost')
    elif comp_draw == 'rock' and word =='paper':
        print('you won!')
    elif comp_draw == 'rock' and word =='scissors':
        print('you lost')
    elif comp_draw == 'scissors' and word =='rock':
        print('you won!')
    elif comp_draw == 'scissors' and word =='rock':
        print('you won!')
    elif comp_draw == 'scissors' and word =='rock':
        print('you won!')


if __name__ == "__main__":
    game()
    while True:
        if input('Would you like to play_again? ') == 'yes':
            game()
        else:
            break
Answered By: Eduard Voiculescu

Put your word variable inside your game function. You can loop it forever. Make a playAgain function then ask the user yes or no. If its starts with "y", restart the game() function, if not end the program. I am using an online compiler so the module sys wont work on my end but it should on yours.

import random
import sys

a = ["rock", "paper", "scissors"]



def playAgain():
    play_again = input('would you like to play again? (y/n): ')
    if play_again.startswith('y'):
        game()
    else:
        sys.exit()

def game():
    word = input('Enter rock, paper, or scissors: ')
    while word not in a:
        word = input('You must enter rock, paper, or scissors. Try again: ')


    comp_draw = random.choice(a)
    print('The computer drew ' + comp_draw)
    if comp_draw == 'rock' and word=='rock':
        print('It was a tie')
        playAgain()
    elif comp_draw == 'paper' and word=='paper':
        print('It was a tie')
        playAgain()
    elif comp_draw == 'scissors' and word=='scissors':
        print('It was a tie')
        playAgain()
    elif comp_draw == 'paper' and word=='rock':
        print('you lost')
        playAgain()
    elif comp_draw == 'rock' and word=='paper':
        print('you won!')
        playAgain()
    elif comp_draw == 'rock' and word=='scissors':
        print('you lost')
        playAgain()
    elif comp_draw == 'scissors' and word=='rock':
        print('you won!')
        playAgain()
    elif comp_draw == 'scissors' and word=='rock':
        print('you won!')
        playAgain()
    elif comp_draw == 'scissors' and word=='rock':
        print('you won!')
        playAgain()
game()
Answered By: Ray953
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.