0
#Word Jumble Game

import random
import string

def jumbled():
words = ['Jumble', 'Star']#, 'Candy', 'Wings', 'Power', 'String', 'Shopping', 'Blonde', 'Steak', 'Speakers', 'Case', 'Stubborn', 'Cat', 'Marker', 'Elevator', 'Taxi', 'Eight', 'Tomato', 'Penguin', 'Custard']
count = 0    
loop = True
loop2 = True
while loop:
word = string.lower(random.choice(words)
jumble = list(word)
random.shuffle(jumble)
scrambled = "".join(jumble) 
while loop2:
    print '\n',scrambled,'\n'
    guess = raw_input('Guess the word: ')
    quit = set(['quit','Quit'])
    choice = 'quit'
    if guess.lower() == word:    
        print '\nCorrect!'
        count+=1
        print "You took",count,"trie(s) in order to get the right answer",jumbled()
    else:
        print '\nTry again!\n'
        count+=1
        if count == 3:
            if words[0]*2:
                print "It looks like you're having trouble!"
                print 'Your hint is: %s'(words) # I'm trying to pull a word from the above list here.



jumbled()

Hi there! How do I go about quitting the game whenever I type in 'quit' or 'Quit'?

Basically I've created a program that gives you a mixed, jumbled up word, then I'll have to guess it correctly. When I get the ability to type, I'd like to type quit so I can exit the game whenever necessary.

Thanks in advance! (You don't need to know how the game works, I just need to find a way to type in quit then the game will quit)

Also, where would I type in the code to make it quit? Please explain this also if you're just giving me the code.

juiceb0xk
  • 949
  • 3
  • 19
  • 46

2 Answers2

0
import sys
guess = raw_input('Guess the word: ')
if guess in ['quit','Quit']:
    print "Goodbye!"
    sys.exit()

See also: Difference between exit() and sys.exit() in Python

Community
  • 1
  • 1
Paul
  • 26,170
  • 12
  • 85
  • 119
  • 1
    why note `guess.trim().lower() = 'quit'` since QUIT quIT qUiT QUIT QUit...etc should all be valid. Or even `guess.trim().lower() in ['q', 'quit']` – Mike McMahon Nov 07 '14 at 05:47
  • 3
    We could also add: stop, exit, bye, goodbye, f***off, die, ^D, i need to go now, for the love of GOD QUIT ALREADY, etc. so between the set test and your suggestion, I think its covered. – Paul Nov 07 '14 at 05:48
  • After I type quit, it keeps looping around to the next jumbled word? – juiceb0xk Nov 07 '14 at 05:58
  • Does it also print "Goodbye!" ? – Paul Nov 07 '14 at 05:59
  • The block here goes inside the loop2 using your existing guess=raw_input line, except the import which goes at the top. – Paul Nov 07 '14 at 06:03
  • Nevermind! Instead of sys.exit, I used loop = False and it quits the loop and stops the program! Thanks for the help guys. – juiceb0xk Nov 07 '14 at 06:03
  • Yeah well `sys.exit()` will work, `sys.exit` won't. Glad you got it working. – Paul Nov 07 '14 at 06:04
0

You can use another elif condition.

import sys
# ...

if guess.lower() == word:    
    print '\nCorrect!'
    # ...
elif guess.lower() == 'quit':
    sys.exit()    
else:
    print '\nTry again!\n'
    # ...
Himal
  • 1,351
  • 3
  • 13
  • 28