我如何通过输入“退出”退出游戏?

2024-09-29 23:22:06 发布

您现在位置:Python中文网/ 问答频道 /正文

#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,游戏就会退出)

另外,我应该在哪里输入代码使它退出?如果你只是给我密码,请解释一下。在


Tags: theimportloop游戏stringifcountrandom
2条回答

您可以使用另一个elif条件。在

import sys
# ...

if guess.lower() == word:    
    print '\nCorrect!'
    # ...
elif guess.lower() == 'quit':
    sys.exit()    
else:
    print '\nTry again!\n'
    # ...
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

相关问题 更多 >

    热门问题