如何输入'quit'退出游戏?

时间:2014-11-07 05:39:33

标签: python quit

#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()
你好!每当我输入'退出'退出游戏时如何退出游戏?或者'退出'?

基本上我已经创建了一个程序,它会给你一个混乱,混乱的单词,然后我必须正确地猜测它。当我有能力输入时,我想输入quit,这样我就可以在必要时退出游戏。

提前致谢! (你不需要知道游戏是如何工作的,我只需要找到一种输入退出方式,然后游戏就会退出)

此外,我会在哪里输入代码以使其退出?如果您只是给我代码,请解释一下。

2 个答案:

答案 0 :(得分:0)

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

另请参阅:Difference between exit() and sys.exit() in Python

答案 1 :(得分:0)

您可以使用其他elif条件。

import sys
# ...

if guess.lower() == word:    
    print '\nCorrect!'
    # ...
elif guess.lower() == 'quit':
    sys.exit()    
else:
    print '\nTry again!\n'
    # ...