0
import math
import random
import time
import sys

Start = False
Quit = False

while True:
    print ("Welcome to my first ever RPG! created 10/07/2016")
    time.sleep(2)
    begin = raw_input("Would you like to start the game?")

    if begin.strip() in ("yes" , "y" , "Yes" , "Y" ):
        Start = True
    break





while Start == True:   
    player_name = raw_input("What would you like to name your character?")
    print ("Welcome " + player_name.capitalize())
    Quit_command = raw_input()
    if Quit_command.strip() in ("q" , "Q"):
        print("Game closing....")
        time.sleep(1)
        Quit = True

    break



while Quit == True:
    sys.exit()

As you can see, I've probably coded this appallingly haha but basically, what I want is to be able to allow the user to quit the program ANYTIME when pressing the 'Q' button during execution of the program

P.S I'm new to python so the simplest solution would be most appreciated, thanks!

  • 1
    What's currently happening when you run this? Noticed your `break` keywords aren't indented correctly and may exit you from some of your loops sooner than you expect. Not only that, but your `while Quit == True: sys.exit()` code isn't needed as when player presses 'q' or "Q" you can just call `sys.exit()` after your `print/time.sleep` code. No need to have a `Quit` variable and a loop cycle for it. – Jonathon Ogden Jul 10 '16 at 23:13

2 Answers2

1

In case of a simple game, you could just rely on prompting user all the time and checking if he responds with Q or other stuff. (So, the input is synchronous to the program.) In this case you could go with the Dan Coates' answer. Though, I would extend his preprocess_user_input function to handle the whole prompt sequence:

import sys

def user_input(prompt):
    """user_input(prompt)

    prompt -- string to prompt the user with

    returns the user's answer to the prompt
    or handles it in special cases
    """

    inp = raw_input(prompt)
    if inp.strip().upper() == "Q":
        print("Bye, gamer!")
        sys.exit()

    return inp

# and use this function everywhere now

begin = user_input("Would you like to start the game?")

if begin.strip() not in ("yes" , "y" , "Yes" , "Y" ):
    print("Ok, maybe next time...")
    sys.exit()

player_name = user_input("What would you like to name your character?")

print("Welcome " + player_name.capitalize())

while True:
    # now we are in the game
    act = user_input("People are celebrating on the north, yelling 'Selecao! Parabens!'\nWould you like to go north?")
    ...process the act...

-- and so on.

You might want to add more options for special cases. And then you need more if branches. And you might wonder "How to do case in Python?". The answer is: "do it with dictionaries". And it is a good way to do it. (In fact, one could build the whole game on dictionaries of options for each situation the user is in.)

But in case of asynchronous input (so, when user presses the keys on his own, without the program prompting him and expecting for input) it is not simple. The easiest way to quit a running Python program is by pressing Ctrl + C. It will send a SIGINT signal to the program. Which Python converts into an exception, called KeyboardInterrupt. (Not sure about signals in Windows, but for Python it should be the same on any OS.) If an exception is not captured by try..except it exits the program with the prompt of the exception. If you need to -- you can capture it and do what you want. (Print a message "Bye, gamer!" and exit the program. This quiestion is another example.)

But for capturing any custom key combination pressed probably one needs a bit more involved method.

Community
  • 1
  • 1
xealits
  • 4,224
  • 4
  • 27
  • 36
0

Architecturally, I think you want to have some kind of helper function that will process any user input and check for "global" options like Q for quit before checking options specific to whatever particular question you're prompting a user for.

As an example, it could look something like this:

begin = raw_input("Would you like to start the game?")
preprocess_user_input(begin)

...

player_name = raw_input("What would you like to name your character?")
preprocess_user_input(player_name)
print ("Welcome " + player_name.capitalize())

...

def preprocess_user_input(user_input):
    if user_input.strip().upper() == 'Q':
        sys.exit()
Dan Coates
  • 303
  • 2
  • 11