简体   繁体   English

对于循环在一次迭代后结束

[英]For loop ending after one iteration

So I had this original piece of code to replace words in a "fill in the blanks" game for aa python class I'm taking: 所以我有一段原始的代码来替换我正在考虑的python类的“填空”游戏中的单词:

while victory == False:
    round0 = ''
    round1 = ''
    round2 = ''
    round3 = ''
    round4 = ''
    round5 = ''
    answerx = listchange()
    while round0 != answerx[0]:
        print ''.join(glvl)
        round0 = raw_input('What word is ___(0)___? ')
    while round1 != answerx[1]:
        print ''.join(num_replace(glvl,0))
        round1 = raw_input('What word is ___(1)___? ')
    while round2 != answerx[2]:
        print ''.join(num_replace(num_replace(glvl,0),1))
        round2 = raw_input('What word is ___(2)___? ')
    while round3 != answerx[3]:
        print ''.join(num_replace(num_replace(num_replace(glvl,0),1),2))
        round3 = raw_input('What word is ___(3)___ ')
    print ''.join(num_replace(num_replace(num_replace(num_replace(glvl,0),1),2),3))
    print 'Congratulations!'
    victory = True

It was rejected because it only applies to four blanks in the question and my code should work no matter how many blank spots are in it. 它被拒绝了,因为它只适用于问题中的四个空格,无论有多少空白点,我的代码都应该有效。

So now I've been working on remaking it and been having a lot of trouble. 所以现在我一直在努力改造它并且遇到了很多麻烦。 Here is my most successful attempt so far: 这是迄今为止我最成功的尝试:

for i in glvl:
    glvlstr = ''.join(glvl)
    answerx = listchange()
    counter = 0
    if i == blank_list[counter]:
        print glvlstr
        askq = raw_input('What word is ' + i + '? ')
        if askq == answerx:
            glvl[i] = askq
            glvlstr = ''.join(glvl)
    counter += 1

It starts fine and runs this: 它开始很好并运行:

Please select a difficulty level: easy, normal, hard, maximum-carnage, bonus-level: easy
In ___(0)___ if you want to pass the W3 ___(1)___ make sure you ___(2)___ your ___(3)___!
What word is ___(0)___? HTML

But at that point it just quits. 但就在那时它就会退出。 It should then print out: 然后打印出来:

In HTML if yuou want to pass the W3 ___(1)___ make sure you ___(2)___ your ___(3)___!
What word is ___(1)___? 

and then cycle through word by word replacing them until it is done. 然后循环逐字逐句替换它们直到完成。

You set counter to zero, check the if statement, then increment the counter. 将counter设置为零,检查if语句,然后递增计数器。 But on the next iteration you reset counter to zero, so the if is always checking for counter = 0. Initialize counter before the for loop. 但是在下一次迭代中,您将计数器重置为零,因此if始终检查counter = 0.在for循环之前初始化计数器。 Seems like this might be your problem. 似乎这可能是你的问题。

As you are incrementing counter for every iteration, you can clean it us a bit as follows 当您为每次迭代递增counter ,您可以按如下方式清除它们

for counter,i in enumerate(glvl):
    # glvlstr = ''.join(glvl) unnecessary
    answerx = listchange() # what does this do?
    if i == blank_list[counter]:
        print ''.join(glvl)
        askq = raw_input('What word is ' + i + '? ')
        if askq == answerx:
            glvl[i] = askq
            # glvlstr = ''.join(glvl) unnecessary

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

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