0

Just need this for a game program. At the end of the Rock-Paper-Scissors program it asks if the user wants to play again or not, and uses sys.exit() to terminate the program. However, instead of terminating the program if the user enters something that isn't 'Y' or 'y' in the replayFunc() function, it goes into the code under the except: block in main(). Any help is appreciated.

import random
import os
import sys

userOS = str(sys.platform)

def clearFunc():
  if "win32" not in userOS:
    os.system('clear')
  else:
    os.system('cls')

def replayFunc():
  exitPrompt = input("Do you want to play again? (Y/N): ")
  if exitPrompt == 'Y' or exitPrompt == 'y':
    main()
  else:
    sys.exit(0)


def main():
  oppChoice = random.randint(1,3)
  playerChoice = int
  print("Welcome to Rock-Paper-Scissors!")
  input("\nPress Enter to continue: ")
  clearFunc()

  try:
    playerChoice = int(input("Please use 1-3 to select from the list below:\n\n1. Rock\n2. Paper\n3. Scissors\n"))
    if playerChoice == 1 and oppChoice == 1:
      print("You tied!")
      replayFunc()
    elif playerChoice == 1 and oppChoice == 2:
      print("Computer Wins!")
      replayFunc()
    elif playerChoice == 1 and oppChoice == 3:
      print("You win!")
      replayFunc()

    elif playerChoice == 2 and oppChoice == 1:
      print("You win!")
      replayFunc()
    elif playerChoice == 2 and oppChoice == 2:
      print("You tied!")
      replayFunc()
    elif playerChoice == 2 and oppChoice == 3:
      print("Computer Wins!")
      replayFunc()

    elif playerChoice == 3 and oppChoice == 1:
      print("Computer wins!")
      replayFunc()
    elif playerChoice == 3 and oppChoice == 2:
      print("You win!")
      replayFunc()
    elif playerChoice == 3 and oppChoice == 3:
      print("You tied!")
      replayFunc()
    else:
      print("Didn't get that")
      main()


  except:
    print("TypeError")
    main()

main()

  • I think you should re-think the flow of the your program. It seems to call `main()` recursively over and over. If the user stays in the game long enough, the stack will be full. In a programming language with tail-call optimization, this may be OK, but Python is definitely not such a language. – Nick Lee Dec 21 '19 at 03:37
  • @NickLee: I have a hard time imagining someone wanting to play hundreds of games of Rock, Paper, Scissors against the computer, but yes, in general this is a bad design. :-) – ShadowRanger Dec 21 '19 at 03:39
  • What would you suggest for a rewrite to make this optimized? –  Dec 21 '19 at 03:49
  • 1
    @JimnorRain: An infinite loop around the repeated code in `main` (`while True:`) with appropriate `break` and `continue` usage in place of recursive calls would avoid the unbounded recursion. – ShadowRanger Dec 21 '19 at 04:05

1 Answers1

2

sys.exit(0) is basically implemented as raise SystemExit(0). It's an exception, because you need to bubble up so context managers (with statements) and finally blocks can clean up properly.

The reason bare except: is frowned upon is because it catches even exceptions that shouldn't be caught, e.g. SystemExit. So stop using it, and catch only exceptions you know how to handle, or at least change it to except Exception: which won't catch the special, shouldn't be caught exceptions (like SystemExit and KeyboardInterrupt).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271