所以我有了这段原始代码,可以将"填空"游戏中的单词替换为我正在学习的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
它被拒绝了,因为它只适用于问题中的四个空格,而且无论其中有多少个空格,我的代码都应该有效
所以现在我一直在重新制作它,遇到了很多麻烦。这是我迄今为止最成功的尝试:
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
它启动良好,运行如下:
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
但在这一点上,它就停止了。然后应该打印出来:
In HTML if yuou want to pass the W3 ___(1)___ make sure you ___(2)___ your ___(3)___!
What word is ___(1)___?
然后逐字逐句地循环替换它们,直到完成为止。
将计数器设置为零,检查if语句,然后递增计数器。但在下一次迭代中,您将counter重置为零,所以if总是检查counter=0。在for循环之前初始化计数器。看来这可能是你的问题。
当您为每次迭代递增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