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()

4
  • 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. Commented Dec 21, 2019 at 3: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. :-) Commented Dec 21, 2019 at 3:39
  • What would you suggest for a rewrite to make this optimized? Commented Dec 21, 2019 at 3: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. Commented Dec 21, 2019 at 4:05

1 Answer 1

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).

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

Comments

Your Answer

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