我尝试在python 2.7中退出程序时出错

时间:2017-01-17 19:13:00

标签: python python-2.7

  

我试过quit(0)和sys.exit

import random 

word = random.choice(open(r"C:\Users\Sigma.jota3\Documents\new.txt").read().split()).strip()
word = list(word)
print ' '.join(word)

word_2 = []

for i in range(len(word)):
    word_2.append("_")

print ' '.join(word_2)


word_3 = [] 


count = 0 


numbers_to_count = len(word)


while True:    

    word_input = raw_input("")     

    word = ''.join(word) 

    for el in range(len(word)):
        if not word[el] != word_input:
            word_3.append(word[el])
            numbers_to_count = numbers_to_count - 1 
            print "your answer is right!, now you have %s letters to resolve!" % numbers_to_count
        if numbers_to_count == 0:
            print "Congratulations, you win!"
            exit(0) 
        if numbers_to_count < 0:
            exit(0) 

    for le in range(len(word)):
        word_word_input = word.find(word_input)  
        if word_word_input < 0: 
            count = count + 1
            print "Try again, you have failed in %s times!" % count
        if count > 7:
            print "Tip: The word have %s letters." % len(word)  
        if count > 12:
            print "You lose!, you made more than %s tries." % count
            exit(0)

1 个答案:

答案 0 :(得分:0)

您的代码中没有import sys。如果您的代码中确实有导入,那么sys.exit() how to use it python我也会在您的代码中看到您使用exit(0)。这只有from sys import exit才有效。但是,既然你说你试过sys.exit(0),那么你可能需要添加import语句。

sys.exit()引发SystemExit例外。这可能就是你所看到的

Traceback (most recent call last):
line xx, in <module>
sys.exit()
SystemExit