2

This is for a game. The game asks the user if s/he would like to play again. If not, the program should just exit. If yes, the entire game is repeated and asks to play again, and so on.

while True:

    print "*game being played*"

    # prompt to play again:

    while True:

        replay = raw_input("Play again? ")

        print replay

        if replay.lower == "yes" or "y":
            break
        elif replay.lower == "no" or "n":
            sys.exit()
        else:
            print "Sorry, I didn't understand that." 

However, when I actually execute this code it acts as if every answer input is a yes (even "aksj;fakdsf"), so it replays the game again.

.

When I changed the code to first consider no instead of yes:

if replay.lower == "no" or "n":
    sys.exit()

I get the error

Traceback (most recent call last):
  File "C:/Python27/Programs/replay game.py", line 18, in <module>
    sys.exit()
NameError: name 'sys' is not defined

This might have something to do with the fact I don't actually know what sys.exit() does but just found it while googling "how to exit program python".

1
  • Thank you guys. No wonder it kept repeating regardless of input. If I had rep I'd vote you all up. Commented Oct 24, 2013 at 1:09

4 Answers 4

3

lower is a function in python.

Be sure to include the elipses (). It should look like string.lower()

Also, try putting it at the end of your input so you don't have to type it every time

replay = raw_input('Play again? ').lower()

As Jon Clements pointed out, something that I looked over and missed in your code, consider the following statement:

if replay.lower() == "yes" or "y":
    #execute

To the human eye, this looks correct, but to the computer it sees:

if replay.lower() is equal to "yes" or if 'y' is True...execute

Your game will always replay because "y" is a string and always true. You must replace the code with something like this (my above advice included):

if replay == 'yes' or replay == 'y':
    #execute

finally, import sys at the top of your program. This is where the error is occurring, because sys is a module that must be imported to the program.

Here is an article on operators that you might benefit reading from

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

4 Comments

Might also want to address that blah == 'yes' or 'y' isn't going to work as expected either...
Okay so it keeps spitting back the input I just gave it. For example, after I input 'n', the terminal writes 'n' again before exiting. How do I get that to stop? EDIT: nvm, my own stupidity :P
I would expect it to, you're telling it to print input. Are you saying the conditionals aren't executing? If that's the case, the only reason it wouldn't work is if you're using .lower instead of .lower()
Haha I'm sorry, I realised that I've been telling it to print input this whole time. Thanks though!
1

You first need to import sys. Place this:

import sys

at the top of your code to import the sys module.

However, a much easier way to exit a script is to just do this:

raise SystemExit

The above code does the exact same thing as sys.exit.

Also, for your code to work properly, you will need to do two more things:

  1. Reconstruct your if-statements to use the in keyword.
  2. Invoke the .lower method by placing () after it.

Below is a fixed version of your script:

while True:

    print "*game being played*"

    # prompt to play again:

    while True:

        # I put .lower() up here so I didn't have to call it multiple times
        replay = raw_input("Play again? ").lower()

        print replay

        if replay in ("yes", "y"):
            break
        elif replay in ("no", "n"):
            raise SystemExit
        else:
           print "Sorry, I didn't understand that."

Now let me explain why you needed to remake your if-statements. As it currently stands, Python is reading your code like this:

if (replay.lower == "yes") or "y":

Furthermore, since "y" is a non-empty string (which always evaluate to True in Python), this if-statement, left as it is, will always pass as True. Using in however like I did above tests whether replay can be found in the tuple ("yes", "y").

Comments

1

At the beginning of the code you have to add:

import sys

then other code can follow

1 Comment

Please make more ovious the addtiional insight this provides, especially when compared to the existing, older, upvoted and better explained answer, which also provides an alternative; the one by user2555451.
0

Firstly, sys is a standard lib package that needs to be imported to reference it. I recommend reading up a bit on importing in python. put this at the top of your code:

import sys

That should take care of the sys namespace error

Secondly you need to understand how python evaluates if statements

if replay.lower == "no" or "n":

which can be broken up into two statements:

if ( (replay.lower == "no") or ("n") ):

the left side will evaluate to False, and the right side will evaluate to True. this is because "n" (or any non 0/non False object) evaluates to True.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.