如何通过输入"退出"退出游戏?


#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()

嘿,你好!每当我输入"退出"或"退出"时,如何退出游戏?

基本上,我已经创建了一个程序,给你一个混合的,混乱的单词,然后我必须猜对它。当我能够打字时,我想打字退出,以便在必要时退出游戏。

提前感谢!(你不需要知道游戏是如何运作的,我只需要想办法输入退出,然后游戏就会退出(

另外,我将在哪里输入代码以使其退出?如果您只是给我代码,也请解释这一点。

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

另请参阅:Python 中 exit(( 和 sys.exit(( 之间的区别

您可以使用

其他elif条件。

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

最新更新