def n():
name = input('What is the missing animal?')
if name == 'dog':
print('Well done')
else:
print('Sorry this is not right')
rep= 0
while rep < 5:
n()
rep = rep + 1
if rep == 5:
print ('You have guessed incorrectly 5 times.)
当我运行这个并得到错误的答案,程序不断重复,而不是重复最多5次。为什么while循环在if循环中不起作用?
任何想法?
递归调用从0开始。您永远不会进入while循环的第二次迭代。 – Prune
'if'不是一个循环;这是一个分支语句。 – chepner