繁体   English   中英

如何输入“ quit”退出游戏?

[英]How do I quit my game by typing in '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”或“ Quit”时,如何退出游戏?

基本上,我创建了一个程序,为您提供了一个混杂的单词,然后我必须正确地猜测它。 获得打字功能后,我想输入quit,这样我就可以在需要时退出游戏。

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

另外,我应该在哪里键入代码使其退出? 如果您只是给我代码,也请解释一下。

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'
    # ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM