Alisha Jain
Alisha Jain

Reputation: 75

How would I code "q to quit"? (Python 3.6)

I've been coding a program with a partner for a class assignment and we've been stuck on adding a "q to quit" in it. The assignment is to make a "guess the number" game. Here's our code:

import random

def get_distance(secret,guess):
    difference = abs(secret - guess)
    return difference
def point_machine(guess1,guess2):
    points_1 = 0
    points_2 = 0
    if guess1 < guess2:
        print ("Player 1 wins")
        points_1 += 1
    elif guess2 < guess1:
        print ("Player 2 wins")
        points_2 += 1
    elif guess1 == guess2:
        print ("It's a tie!")
        points_1 += 1
        points_2 += 1
    return points_1, points_2

x = 0
print ("Welcome to Guess-the-Number!")
print ("Press 'q' to quit at any time.")
name1 = input("What is player one's name?: ")
name2 = input("What is player two's name?: ")
rounds = (input("How many rounds?: "))
points_1 = 0
points_2 = 0
for i in range(int(rounds)):
    x += 1
    secret = random.randint(0,100)
    print ("Round",x)
    print ("~~~~~~~")
    guess1 = int(input(name1+", take a guess: "))
    guess1 = get_distance(secret,guess1)
    guess2 = int(input(name2+", take a guess: "))
    guess2 = get_distance(secret,guess2)
    new1,new2=point_machine(guess1,guess2)
    points_1 += new1
    points_2 += new2
    print ("The number was:",secret)
    print ("Score:",points_1,"to",points_2)

if points_1 > points_2:
    print (name1,"wins the game!")
elif points_2 > points_1:
    print (name2,"wins the game!")
elif points_1 == points_2:
    print ("It's a tie!")
    print (points_1,"to",points_2)

The requirement is that when "q" is inputted at any time during the game, it returns you to the "main menu".

Thanks in advance

Upvotes: 0

Views: 3738

Answers (2)

alexisdevarennes
alexisdevarennes

Reputation: 5642

You could use pythoncom, pyHook

import pythoncom, pyHook
import random

def get_distance(secret,guess):
    difference = abs(secret - guess)
    return difference

def point_machine(guess1,guess2):
    points_1 = 0
    points_2 = 0
    if guess1 < guess2:
        print ("Player 1 wins")
        points_1 += 1
    elif guess2 < guess1:
        print ("Player 2 wins")
        points_2 += 1
    elif guess1 == guess2:
        print ("It's a tie!")
        points_1 += 1
        points_2 += 1
    return points_1, points_2

def start_game():
    x = 0
    print ("Welcome to Guess-the-Number!")
    print ("Press 'q' to quit at any time.")
    name1 = input("What is player one's name?: ")
    name2 = input("What is player two's name?: ")
    rounds = (input("How many rounds?: "))
    points_1 = 0
    points_2 = 0
    for i in range(int(rounds)):
        x += 1
        secret = random.randint(0,100)
        print ("Round",x)
        print ("~~~~~~~")
        guess1 = int(input(name1+", take a guess: "))
        guess1 = get_distance(secret,guess1)
        guess2 = int(input(name2+", take a guess: "))
        guess2 = get_distance(secret,guess2)
        new1,new2=point_machine(guess1,guess2)
        points_1 += new1
        points_2 += new2
        print ("The number was:",secret)
        print ("Score:",points_1,"to",points_2)

    if points_1 > points_2:
        print (name1,"wins the game!")
    elif points_2 > points_1:
        print (name2,"wins the game!")
    elif points_1 == points_2:
        print ("It's a tie!")
        print (points_1,"to",points_2)

def OnKeyboardEvent(event):

    if event.Key in ['q', 'Q']:
        start_game()


    # return True to pass the event to other handlers
    return True

# create a hook manager
hm = pyHook.HookManager()
# watch for all input events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()

start_game()

Upvotes: 0

theParanoidAndroid
theParanoidAndroid

Reputation: 407

You will need to test at every input() command. So instead of:

guess1 = int(input(name1+", take a guess: "))
guess1 = get_distance(secret,guess1)
guess2 = int(input(name2+", take a guess: "))
guess2 = get_distance(secret,guess2)

Try this:

userInput = input(name1+", take a guess: ")
if userInput == "q":
    exit()

guess1 = int(userInput)
guess1 = get_distance(secret,guess1)

userInput = input(name2+", take a guess: ")
if userInput == "q":
    exit()

guess2 = int(userInput)
guess2 = get_distance(secret,guess2)

This way, at any time, the user can type "q" and hit enter to quit the game.

Or, alternatively, you can define your own function to use instead of the input() function:

def userInput(prefix):
    a = input(prefix)
    if a=="q":
        exit()
    else:
        return a

And then:

guess1 = int(userInput(name1+", take a guess: "))
guess1 = get_distance(secret,guess1)
guess2 = int(userInput(name2+", take a guess: "))
guess2 = get_distance(secret,guess2)

Upvotes: 2

Related Questions