当if语句为false时,循环被中断

时间:2016-04-19 06:04:48

标签: python-2.7 if-statement while-loop

我有这段代码,用于询问空白字应该是什么,然后如果您正确猜测则转到下一个。那部分工作正常,但如果你输入错误的答案,它应该再次提出问题,但不是这样做的。相反,python shell只是空白,没有错误,没有。我无法看到问题:

while victory == False:
    if askq == answerx[counter]:
        newstring = newstring.replace(blank_list[counter],answerx[counter])
        counter += 1
        print newstring
        if counter < len(blank_list):
            askq = raw_input('What word is ' + blank_list[counter] + '? ')
    if newstring == answers:
        print 'Congratulations!'
        victory = True

1 个答案:

答案 0 :(得分:1)

我想我明白了!可能有更好的方法,但这只是有效:

while victory == False:
    if askq == answerx[counter]:
        newstring = newstring.replace(blank_list[counter],answerx[counter])
        counter += 1
        print newstring
        if counter < len(blank_list):
            askq = raw_input('What word is ' + blank_list[counter] + '? ')
    else:
        print newstring
        askq = raw_input('What word is ' + blank_list[counter] + '? ')
    if newstring == answers:
        print 'Congratulations!'
        victory = True