-1

Hey I'm completely new to Python, only been using it for 2 days, and relatively new to programming. I'm trying to create a Rock, Paper, Scissors game. My problem is that when I run the program and I insert 'quit' (which is supposed to exit the program) the program keeps running, and I have no idea why. Any ideas? This is my code:

from random import randint

#the computer's hand
def ran_number():
  comp_choice = randint(1, 3)
  if comp_choice == 1:
    hand = "rock"
  elif comp_choice == 2:
    hand = "paper"
  else:
    hand = "scissors"
  return hand

#game starts
def play_game():
  print("Let's play a game of Rock, Paper, Scissors!")
  while True:
    choice = input("Type: 'rock', 'paper', or 'scissors' to play, or 'quit' to end the game.")
    choice.lower()
    comp = ran_number()

    if choice == 'quit':
      print("\nIt's been a pleasure to play against you! Hope to see you another time.")
      break
    elif ((choice == 'rock' and comp == 'paper') or (choice == 'paper' and comp == 'scissors') or (choice == 'scissors' and comp == 'rock')):
      print("\n{} beats  {}! You lose!".format(comp.capitalize(), choice.capitalize()))
      play_game()
      continue
    elif choice == comp:
      print("\nYou both played {}. It's a tie!".format(choice.capitalize()))
      play_game()
      continue
    else:
      print("\n{} beats{}! You win!".format(choice.capitalize(), comp.capitalize()))
      play_game()
      continue


play_game()
D. Stenson
  • 27
  • 1
  • 3
  • `choice.lower()` after you get the input should be `choice = choice.lower()'. I'm not sure that's you issue though; I would guess you're typing 'quit' in lowercase anyway. – Cyphase Aug 13 '15 at 10:18
  • @Cyphase: Correct, but the main problem here is due to `play_game()` calling itself, as noted by Anand & Funky. – PM 2Ring Aug 13 '15 at 10:31

3 Answers3

1

The issue is that you recurse into your play_game() function each time. So when you give quit input, it exits out of one level of recursion, you have to give quit input continously for the amount of times you played the game for the game to quit.

You do not really need to do recursion at all, just remove it and it should be fine, since you are also using while loop.

Also, another thing - choice.lower() is not in-place, you should assign it back to choice .

Also, you do not need the continue inside the elif part, since you do not have any statements after the continue anyway, so it would automatically continue the loop.

Code -

from random import randint

#the computer's hand
def ran_number():
    comp_choice = randint(1, 3)
    if comp_choice == 1:
        hand = "rock"
    elif comp_choice == 2:
        hand = "paper"
    else:
        hand = "scissors"
    return hand

#game starts
def play_game():
    print("Let's play a game of Rock, Paper, Scissors!")
    while True:
        choice = input("Type: 'rock', 'paper', or 'scissors' to play, or 'quit' to end the game.")
        choice = choice.lower()
        comp = ran_number()

        if choice == 'quit':
            print("\nIt's been a pleasure to play against you! Hope to see you another time.")
            break
        elif ((choice == 'rock' and comp == 'paper') or (choice == 'paper' and comp == 'scissors') or (choice == 'scissors' and comp == 'rock')):
            print("\n{} beats  {}! You lose!".format(comp.capitalize(), choice.capitalize()))
        elif choice == comp:
            print("\nYou both played {}. It's a tie!".format(choice.capitalize()))
        else:
            print("\n{} beats{}! You win!".format(choice.capitalize(),  comp.capitalize()))

play_game()
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
0

I think you want to say

choice = choice.lower()

Also it might be safer to do

choice = choice.lower().strip()

To remove any unwanted whitespace.

JoeCondron
  • 8,546
  • 3
  • 27
  • 28
0

Import sys module and invoke the exit() function to exit the program.

import sys

choice = raw_input("Type: 'rock', 'paper', or 'scissors' to play, or 'quit' to end the game: ")

if choice == 'quit':
    print("\nIt's been a pleasure to play against you!\n"
          "Hope to see you another time.")

sys.exit()
perror
  • 7,071
  • 16
  • 58
  • 85