Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:34:24+00:00 2026-06-11T11:34:24+00:00

Building a simple Python game of Rock, Paper, Scissors for learning purposes. I’ve read

  • 0

Building a simple Python game of “Rock, Paper, Scissors” for learning purposes.

I’ve read some other posts here on exiting from Python without a traceback. I’m trying to implement it but still getting a traceback! Can some Python wiz point out what’s wrong for this Python dummy? The idea is that clicking RETURN (or typing “yes” or “y” will make the program run the play() again, but hitting CTRL-C will close it without a traceback. I´m using Python 2.7.

    # modules
    import sys, traceback
    from random import choice

    #set up our lists
    ROCK, PAPER, SCISSORS = 1, 2, 3
    names = 'ROCK', 'PAPER', 'SCISSORS'

    #Define a function for who beats who?
    def beats(a, b):
        return (a,b) in ((PAPER, ROCK), (SCISSORS, PAPER), (ROCK, SCISSORS))

    def play():
        print "Please select: "
        print "1 Rock"
        print "2 Paper"
        print "3 Scissors"
        # player choose Rock, Paper or Scissors
        player_choice = int(input ("Choose from 1-3: "))
        # assigns the CPU variable a random CHOICE from a list.
        cpu_choice = choice((ROCK, PAPER, SCISSORS))

        if cpu_choice != player_choice:
            if beats(player_choice, cpu_choice):
                print "You chose %r, and the CPU chose %r." % (names[player_choice - 1], names[cpu_choice - 1])
                print "You win, yay!!"
            else:
                print "You chose %r, and the CPU chose %r." % (names[player_choice - 1], names[cpu_choice - 1])
                print "You lose. Yuck!"
        else:
            print "You chose %r, and the CPU chose %r." % (names[player_choice - 1], names[cpu_choice - 1])
            print "It's a tie!"

        print "Do you want to play again? Click RETURN to play again, or CTRL-C to exit!"

        next = raw_input("> ")

        # THIS IS WHAT I'M WORKING ON - NEED TO REMOVE TRACEBACK!
        if next == "yes" or "y":
            try:
                play()
            except KeyboardInterrupt:
                print "Goodbye!"
            except Exception:
                traceback.print_exc(file=sys.stdout)
            sys.exit(0)
        elif next == None:
            play()
        else:
            sys.exit(0)

# initiate play() !
play()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:34:25+00:00Added an answer on June 11, 2026 at 11:34 am

    You call play() twice, so you need to put both cases in a try/except block:

    if next in ("yes", "y"):
        try:
            play()
        except KeyboardInterrupt:
            print "Goodbye!"
        except Exception:
            traceback.print_exc(file=sys.stdout)
        sys.exit(0)
    elif next is None:
        try:
            play()
        except KeyboardInterrupt:
            print "Goodbye!"
        except Exception:
            traceback.print_exc(file=sys.stdout)
            sys.exit(0)
    else:
        sys.exit(0)
    

    I’ve corrected two other problems, it is better to test for None with is in python, and your first if test was not going to work, as next == "yes" or "y" is interpreted as next == "yes" separately from "y" with an or in between. The "y" is always seen as True so you never would come to the other branches in your code.

    Note that I suspect the above code could be simplified much more, but you don’t show us your play() function at all, so you leave us to guess what you are trying to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a simple echo server for the purposes of learning the fundamentals
I am learning Python by building a simple PyGTK application that fetches data from
I've been learning python for use in ArcGIS and some other non-web applications. However,
I'm building a simple interpreter in python and I'm having trouble handling differing numbers
I am building a simple python CGI script to work with Twilio. Right now,
I'm building a simple web-based RSS reader in Python, but I'm having trouble parsing
I've been learning python for a while now with some success. I even managed
I'm building a simple Python raytracer with pure Python (just for the heck of
Im building a simple web app in Python using web.py - and was wondering
I am building simple notificiation system and I just wanted to know what is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.